CMinusCompiler
lexical analyzer and the parser for the c- language
Installation instructions
- download javcc from https://javacc.org/download
- extract archive to the location c:\
- confirm the availability of C:\javacc-6.0\bin\lib\javacc.jar
- to generate lexical analyzer, parser and supporting scripts:
java -cp C:\javacc-6.0\bin\lib\javacc.jar javacc exparser.jj
- to compile scripts:
javac *.java
- to parse c- language script:
java CMParser input.txt
Example program of c- language
/* A program to perform Euclid's
Algorithm to compute gcd.*/
int i;
int gcd (int u, int v)
{
if(v==0) return u ;
else return gcd(v, u-u/v*v);
/* u-u/v*v == u mod v*/
}
void main(void)
{
int x; int y;
x = input(); y = input();
output(gcd(x, y));
}
Syntax rules for c-
-
program -> declaration-list
-
declaration-list -> declaration-list declaration | declaration
-
declaration -> var-declaration | fun-declaration
-
var-declaration-> type-specifier ID ; | type-specifier ID [ NUM ] ;
-
type-specifier-> int | void
-
fun-declaration-> type-specifier ID ( params ) compound-stmt
-
params-> param-list | VOID
-
param-list-> param-list , param| param
-
param-> type-specifier ID | type-specifier ID [ ]
-
compound-stmt-> { local-declarations statement-list }
-
local-declarations-> local-declarations var-declaration | empty
-
statement-list-> statement-list statement | empty
-
statement-> expression-stmt | compound-stmt | selection-stmt | iteration-stmt | return-stmt
-
expression-stmt-> expression ; | ;
-
selection-stmt-> if( expression ) statement | if( expression ) statement else statement
-
iteration-stmt-> while ( expression ) statement
-
return-stmt-> return ; | return expression ;
-
expression-> var = expression | simple-expression
-
var-> ID | ID [ expression ]
-
simple-expression -> additive-expression relop additive-expression| additive-expression
-
relop-> < | <= | > | >= | == | !=
-
additive-expression-> additive-expression addop term | term
-
addop-> + | -
-
term-> term mulop factor | factor
-
mulop-> * | /
-
factor-> ( expression ) | var | call | NUM
-
call-> ID ( args )
-
args-> arg-list | empty
-
arg-list-> arg-list , expression | expression