我正在为用 ANTLR4 编写的 Decaf 编程语言创建解析器和词法分析器规则。 我正在尝试运行一个解析器测试文件,通过在终端窗口上打印访问过的节点并将它们粘贴到D3_parser_tree.html类中来获取解析器树。 根据此测试文件,当前解析器树缺少带有数字 10 的右方括号: class p ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在使用Antlr语法DECAF创建DECAF的解析器;
//********* LEXER ******************
LETTER: ('a'..'z'|'A'..'Z') ;
DIGIT : '0'..'9' ;
ID : LETTER( LETTER | DIGIT)* ;
NUM: DIGIT(DIGIT)* ;
COMMENTS: '//' ~('\r' | '\n' )* -> channel(HIDDEN);
WS : [ \t\r\n\f | ' '| '\r' | '\n' | '\t']+ ->channel(HIDDEN);
CHAR: (LETTER|DIGIT|' '| '!' | '"' | '#' | '$' | '%' | '&' | '\'' | '(' | ')' | '*' | '+'
| ',' | '-' | '.' | '/' | ':' | ';' | '<' | '=' | '>' | '?' | '@' | '[' | '\\' | ']' | '^' | '_' | '`'| '{' | '|' | '}' | '~'
'\t'| '\n' | '\"' | '\'');
// ********** PARSER *****************
program : 'class' 'Program' '{' (declaration)* '}' ;
declaration: structDeclaration| varDeclaration | methodDeclaration ;
varDeclaration: varType ID ';' | varType ID '[' NUM ']' ';' ;
structDeclaration : 'struct' ID '{' (varDeclaration)* '}' ;
varType: 'int' | 'char' | 'boolean' | 'struct' ID | structDeclaration | 'void' ;
methodDeclaration : methodType ID '(' (parameter (',' parameter)*)* ')' block ;
methodType : 'int' | 'char' | 'boolean' | 'void' ;
parameter : parameterType ID | parameterType ID '[' ']' ;
parameterType: 'int' | 'char' | 'boolean' ;
block : '{' (varDeclaration)* (statement)* '}' ;
statement : 'if' '(' expression ')' block ( 'else' block )?
| 'while' '(' expression ')' block
|'return' expressionA ';'
| methodCall ';'
| block
| location '=' expression
| (expression)? ';' ;
expressionA: expression | ;
location : (ID|ID '[' expression ']') ('.' location)? ;
expression : location | methodCall | literal | expression op expression | '-' expression | '!' expression | '('expression')' ;
methodCall : ID '(' arg1 ')' ;
arg1 : arg2 | ;
arg2 : (arg) (',' arg)* ;
arg : expression;
op: arith_op | rel_op | eq_op | cond_op ;
arith_op : '+' | '-' | '*' | '/' | '%' ;
rel_op : '<' | '>' | '<=' | '>=' ;
eq_op : '==' | '!=' ;
cond_op : '&&' | '||' ;
literal : int_literal | char_literal | bool_literal ;
int_literal : NUM ;
char_literal : '\'' CHAR '\'' ;
bool_literal : 'true' | 'false' ;
当我输入时:
class Program {
void main(){
return 3+5 ;
}
}
解析树无法正确构建,因为它无法将3 + 5识别为表达式。 我的语法是否有引起问题的错误?
Lexer规则从上到下匹配。 当2个或更多lexer规则匹配相同数量的字符时,首先定义的一个将获胜 。 因此,一位整数将作为DIGIT
而不是NUM
匹配。
尝试解析以下内容:
class Program {
void main(){
return 33 + 55 ;
}
}
将被解析就好了。 这是因为33
和55
被匹配为NUM
,因为NUM
现在可以匹配2个字符( DIGIT
仅1个字符,所以NUM
wins )。
要修复此问题,请使DIGIT
成为片段(以及LETTER
):
fragment LETTER: ('a'..'z'|'A'..'Z') ;
fragment DIGIT : '0'..'9' ;
ID : LETTER( LETTER | DIGIT)* ;
NUM: DIGIT(DIGIT)* ;
Lexer片段仅由其他lexer规则在内部使用,并且永远不会成为其自身的标记。
还有两件事:您的WS
规则匹配得太多(现在也匹配了|
和'
),应该是:
WS : [ \t\r\n\f]+ ->channel(HIDDEN);
而且您不应该在解析器中匹配char文字:在lexer中进行匹配:
CHAR : '\'' ( ~['\r\n\\] | '\\' ['\\] ) '\'';
否则,将无法正确解析以下内容:
class Program {
void main(){
return '1';
}
}
因为1
将被标记为NUM
而不是CHAR
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.