Grammar for uC (micro-C) * sestoft@dina.kvl.dk * 2001-03-18 ----------------------------------------------------------- Micro-C (uC) is a sublanguage of C. By design, a syntactically well-formed uC program is also a syntactically well-formed C program, and the intention is that the meaning of the program should be the same in both languages. Many simplifications have been made: * datatypes: int and char variables, arrays, and pointers * no structs, unions, doubles, function pointers, ... * no initializers in variable declarations * functions can return only int, char, void Generating and compiling the lexer and parser for uJava: mosmllex Clex.lex mosmlyac -v Cpar.grm mosmlc -c Absyn.sml mosmlc -c -liberal Cpar.sig Cpar.sml mosmlc -c Clex.sml Grammar ------- program ::= program vardecs functiondecs vardecs ::= global variable declarations vardec vardecs functiondecs ::= function declarations functiondec functiondecs vardec ::= variable or parameter declaration typ vardesc vardesc ::= variable description NAME variable ( vardesc ) parenthesized variable description * vardesc pointer to vardesc [ ] array of vardesc [ expr ] array of (with allocation) functiondec ::= function declaration void NAME(parameterdecs) block typ NAME(parameterdecs) block typ ::= int integer type char character type parameterdecs ::= comma-separated parameter list parameterdec1 pardec1 ::= vardec vardec , vardec1 stmt ::= if (expr) stmt if-statement if (expr) stmt else stmt if-else statement while (expr) stmt while-loop expr ; expression as statement return ; return return expr ; return block block statement block ::= block statement { stmtordecseq } stmtordecseq ::= statements and declarations empty sequence stmt stmtordecseq statement vardec stmtordecseq local variable declaration expr ::= access access access = expr assignment const constant literal NAME(exprs) function call ( expr ) parenthesized expression & access address of ! expr logical negation expr op expr arithmetic, comparison, logical print expr print integer expression access ::= NAME local or global variable * access pointer dereferencing access[expr] array indexing exprs ::= comma-separated expressions expr1 expr1 ::= expr expr, expr1 const ::= constant literals CSTINT integer literal - CSTINT negative integer NULL the NULL literal Lexical matters: tokens and comments ------------------------------------ NAME: [`a`-`z``A`-`Z`][`a`-`z``A`-`Z``0`-`9`]* except for the keywords, which are: char else false for if int null return true void while CSTINT: [0-9]+ CSTBOOL: false | true CSTSTRING: `"`...`"` with string escapes \b \t \n \r \" \' \\ \ddd OP: + - * / % == != < <= > >= && || Two kinds of comments (not inside strings, not nested): // comment extends to end of line /* ... */ delimited comment