Giter Club home page Giter Club logo

minimal_c's Introduction

Minimal C

Requirements

  • flex
  • yacc / bison

Basic C programming constructs

  1. Identifiers
  2. Data types: Primitive data types, such as int, float/real, characters, etc.
  3. Constants: Integer, float, string, and character literals
  4. Operators: +,-,*,/,++,--
  5. Statements:
    • Simple statements, break, and return
    • Control flow: if-else, if-else if, while, for
  6. Comments: Single-line (//) and multi-line (/*.. */)

Execution

make compiler

Test a right program

./compiler test/right.c

Test a wrong program

./compiler test/wrong.c

Lexical Tokens

'(' 
')'
'*'
'+'
','
'-'
'/'
';'
'='
'{'
'}'
IDENTIFIER
CONSTANT 
INC_OP  -> ++
DEC_OP  -> --
CHAR 
INT 
LONG 
FLOAT 
DOUBLE 
CONST 
VOID 
IF 
ELSE 
WHILE
FOR 
CONTINUE 
BREAK 
RETURN 

Yacc Grammer

$accept → translation_unit $end
translation_unit → external_declaration
                 | translation_unit external_declaration

external_declaration → function_definition
                     | declaration

declaration → type_specifier init_declarator_list ';'
            | CONST type_specifier init_declarator_list ';'

init_declarator_list → init_declarator
                     | init_declarator_list ',' init_declarator

init_declarator → IDENTIFIER
                | IDENTIFIER '=' assignment_expression

function_definition → type_specifier IDENTIFIER '(' ')' compound_statement

compound_statement → '{' '}'
                   | '{' statement_list '}'
                   | '{' declaration_list '}'
                   | '{' declaration_list statement_list '}'

declaration_list → declaration
                 | declaration_list declaration

statement_list → statement
               | statement_list statement

statement → compound_statement
          | expression_statement
          | selection_statement
          | jump_statement
          | iteration_statement

primary_expression → IDENTIFIER
                   | CONSTANT

postfix_expression → primary_expression
                   | postfix_expression INC_OP
                   | postfix_expression DEC_OP

unary_expression → postfix_expression
                 | INC_OP unary_expression
                 | DEC_OP unary_expression

multiplicative_expression → multiplicative_expression '*' unary_expression
                          | multiplicative_expression '/' unary_expression
                          | unary_expression

additive_expression → multiplicative_expression
                    | additive_expression '+' multiplicative_expression
                    | additive_expression '-' multiplicative_expression

assignment_expression → additive_expression
                      | unary_expression '=' assignment_expression

expression → assignment_expression
           | expression ',' assignment_expression

type_specifier → VOID
               | CHAR
               | INT
               | LONG
               | FLOAT
               | DOUBLE

expression_statement → ';'
                     | expression ';'

selection_statement → IF '(' expression ')' statement
                    | IF '(' expression ')' statement ELSE statement

iteration_statement → WHILE '(' expression ')' statement
                    | FOR '(' expression_statement expression_statement ')' statement
                    | FOR '(' expression_statement expression_statement expression ')' statement

jump_statement → CONTINUE ';'
               | BREAK ';'
               | RETURN ';'
               | RETURN expression ';'

References

minimal_c's People

Contributors

deba-iitbh avatar

Stargazers

Eason Wang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.