Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- // * Java declarations
- // This code is copied in the beginning of the generated Java file
- import scanner.Scanner;
- %}
- // * Yacc declarations
- // Token definition
- %token INT_CONSTANT CHAR_CONSTANT REAL_CONSTANT
- %token INT DOUBLE CHAR VOID STRUCT
- %token ID IF ELSE
- %token MAIN RETURN WHILE READ WRITE
- %token AND OR
- %token EQUALS GREATEREQUAL LOWEREQUAL DISTINCT INCREMENT
- //lower precedence
- %right '='
- %left AND OR '!'
- %left '>' '<' EQUALS DISTINCT GREATEREQUAL LOWEREQUAL
- %left '+' '-'
- %left '*' '/' '%'
- %right UNARY_MINUS
- %nonassoc '(' ')'
- %nonassoc '[' ']'
- %nonassoc '.'
- %right THEN ELSE
- %%
- // * Actions
- program: varDef program
- | functionDef program
- | VOID MAIN '('')' '{' varDefs statements '}'
- ;
- functionDef: type ID '(' params ')' '{' varDefs statements '}'
- | VOID ID '(' params ')' '{' varDefs statements '}'
- ;
- varDefs: varDef varDefs
- | /* empty */
- ;
- varDef: type listIDs ';'
- ;
- type: INT
- | DOUBLE
- | CHAR
- | structConstructor
- | type '[' INT_CONSTANT ']'
- ;
- structConstructor: STRUCT '{' varDefs '}' ;
- listIDs: listIDs ',' ID
- | ID
- ;
- params:
- | listOfParams
- ;
- listOfParams: listOfParams ',' type ID
- | type ID
- ;
- statements: statements statement
- | /* empty */
- ;
- statement: assignment
- | returnStatement
- | ID '.' ID '=' expression ';'
- | ID arrayConstructor '=' expression ';'
- | writeStatement ';'
- | readStatement ';'
- | ifElseStatement ';'
- | whileStatement
- | functionInvocation ';'
- ;
- ifElseStatement: IF '(' expression ')' statement;
- functionInvocation: ID '(' expressions ')'
- ;
- expressions: expression expressionAux
- | /* empty */
- ;
- expressionAux: ',' expression expressionAux
- | /* empty */
- ;
- arrayConstructor :'['expression']' arrayConstructor
- | '['expression']'
- ;
- writeStatement: WRITE expressions
- ;
- readStatement: READ expressions
- ;
- returnStatement: RETURN expression ';'
- whileStatement: WHILE '(' expression ')' '{' statements '}'
- ;
- cast: '(' type ')' expression
- ;
- expression: expression '+' expression
- | expression '*' expression
- | expression '-' expression
- | expression '/' expression
- | expression '%' expression
- | expression '>' expression
- | expression '<' expression
- | '-' expression %prec UNARY_MINUS
- | '!' expression
- | expression AND expression
- | expression OR expression
- | expression EQUALS expression
- | expression GREATEREQUAL expression
- | expression LOWEREQUAL expression
- | expression DISTINCT expression
- | cast
- | INT_CONSTANT
- | CHAR_CONSTANT
- | REAL_CONSTANT
- | ID
- | '(' expression ')'
- | functionInvocation
- | returnStatement
- ;
- assignment: expression '=' expression ';'
- %%
- // * Code
- // Members of the generated Parser class
- // We must implement at least:
- // - int yylex()
- // - void yyerror(String)
- // * Lexical analyzer
- private Scanner scanner;
- // * Root node of the tree
- private ASTNode astRootNode;
- // * Invocation to the scanner
- private int yylex () {
- int token=0;
- try {
- token=scanner.yylex();
- this.yylval = scanner.getYylval();
- } catch(Throwable e) {
- System.err.println ("Lexical error in line " + scanner.getLine()+
- " and column "+scanner.getColumn()+":\n\t"+e);
- }
- return token;
- }
- // * Syntax error handler
- public void yyerror (String error) {
- System.err.println ("Syntax error in line " + scanner.getLine()+
- " and column "+scanner.getColumn()+":\n\t"+error );
- Token lastToken = (Token) yyval;
- System.err.println ("Token" + lastToken.getToken() + "lexema "
- + lastToken.getLexeme() + " start " + lastToken.getStart());
- }
- // * Constructor
- public Parser(Scanner scanner) {
- this.scanner= scanner;
- //this.yydebug = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement