The code for parser that I had:
funcall: ID LB exp? RB ;
exp: funcall | INTLIT ;
Then my assignment requires that I need to do like this for the "funcall":
An invocation expression is a function call which starts with an identifier followed by “(“ and “)”. A nullable comma-separated list of expressions might be appeared between “(“ and “)” as a list of arguments.
And for the "exp", it must follow the Precedence and Associativity:
So I tried to write my code like this to match the requirements (I'm not so sure the order of literals after "funcall" because it's not mentioned in the assignment):
funcall: ID LB (exp (COMMA exp)*)? RB ;
exp
: LB exp RB
| LSB exp RSB
| <assoc=right> (SUB | NOT) exp
| exp op = (DIV | MUL | MODUL) exp
| exp op = (ADD | SUB) exp
| exp op = (LT | LTOE | GT | GTOE) exp
| exp op = (EQUAL | NOEQUAL) exp
| exp AND exp
| exp OR exp
| <assoc=right> exp ASSIGN exp
| funcall
| (INTLIT | FLOATLIT)
| (TRUE | FALSE)
| ID
| STRINGLIT ;
But then I got the error like this, which prevent me from using notation * with "(COMMA exp)" or even "exp" itself:
Here is code for class ASTGeneration where I get error:
override def visitFuncall(ctx:FuncallContext) = CallExpr(ctx.ID.getText,if (ctx.exp == null) List() else List(visit(ctx.exp).asInstanceOf[Expr]))
This is my lexer code that involved:
LB: '(' ;
RB: ')' ;
LP: '{' ;
RP: '}' ;
SEMI: ';' ;
LSB: '[' ;
RSB: ']' ;
COMMA: ',' ;
ADD: '+' ;
SUB: '-' ;
MUL: '*' ;
DIV: '/' ;
OR: '||' ;
AND: '&&' ;
NOT: '!' ;
NOEQUAL: '!=' ;
EQUAL: '==' ;
MODUL: '%' ;
ASSIGN: '=' ;
LT: '<' ;
LTOE: '<=' ;
GT: '>' ;
GTOE: '>=' ;
TRUE: 'true' ;
FALSE: 'false' ;
INTLIT: [0-9]+ ;
FLOATLIT: INTLIT DOT INTLIT ;
fragment DOT: '.' ;
ID: [_a-zA-Z] [_a-zA-Z0-9]* ;
STRINGLIT: '"' ('\\' [bfrnt'"\\] | ~[\b\f\r\n\t'"\\])*; '"' ;
WS : [ \t\r\n]+ -> skip ;
Anyone can tell me what was wrong in my parser code (now I'm wondering about my "exp" could lead to the error in "funcall") and which way should I fix it. Thanks in advance!!