Antlr 解析以特定关键字开头和结尾的代码块
接下来,我想解析以特定关键字开头的代码块(例如,Antlr 解析以特定关键字开头和结尾的代码块,antlr,antlr4,Antlr,Antlr4,接下来,我想解析以特定关键字开头的代码块(例如,,,,…),以关键字结尾。在这两者之间,有些语句应该以分号或新行结尾。到目前为止,我所做的工作可以在以下几方面看到: grammar <garammarName> // Parser Rules statement: EndOfStatment; statement_list: statement+; section: '<firstKeyword>' statement_list End | '&l
,
,
,…),以关键字结尾。在这两者之间,有些语句应该以分号或新行结尾。到目前为止,我所做的工作可以在以下几方面看到:
grammar <garammarName>
// Parser Rules
statement: EndOfStatment;
statement_list: statement+;
section:
'<firstKeyword>' statement_list End
| '<secondKeyword>' statement_list End
| '<thirdKeyword>' statement_list End
sections: section+ EOF;
// Lexer Rules
End: 'End';
NewLine: ('\r'? '\n' | '\n' | '\r') -> skip;
WhiteSpace: [ \t\r\n]+ -> skip;
EndOfStatment: ';' | NewLine;
不会返回任何错误
grun <garammarName> sections -tree < <exampleFile>
grun节-树<
如果您能帮助我了解问题以及如何解决问题,我将不胜感激。当我运行类似于您在此处给出的输入时,我得到:
➜ grun ElmerSolver sections -tree < examples/ex001.sif
line 6:0 missing 'End' at 'Equation'
(sections (section Simulation statement_list End) (section Constants statement_list <missing 'End'>) (section Equation 1 statement_list End) <EOF>)
((我对EndOfStatement
Lexer规则进行了更改)
这是我使用的输入文件:
Simulation
End
Constants
Equation 1
End
这是我使用-gui
grun选项获得的图形视图
回复:使用结束状态
规则进行更改
EndofStatement可能应该是一个解析器规则(小写)
此外,根据语法,始终将“\n”识别为换行符
标记,并使用->skip
规则将其排除在标记流之外
使用-tokens
选项运行grun
,您将不会看到EndOfStatement
标记。(除非在源文件中添加“;”)
➜ grun ElmerSolver节-树-标记
如果希望换行符在语法上有意义(即可以在语法中使用它),则需要删除->skip
然而,一旦你这样做了,你就必须明确新行有效的所有地方(但是我看到你的换行标记,所以看起来这应该有一点Python的感觉,所以这可能就是你想要的)。(同样的注释是:->skip
适用于此)路由,了解Pythongs EOL和缩进处理是众所周知的为解析器添加的(并且“最终的ANTLR 4参考”有一节专门介绍如何处理它)。您也可以在处参考Python语法。我不熟悉antlr的语法符号,但管道符号:|是否可能仅应用于符号而不是整个序列…可能需要使用某种括号?例如:(''语句_列表结束)|(…)|(…)
grammar ElmerSolver;
// Parser Rules
// eostmt: ';' | CR;
statement: EndOfStatment;
statement_list: statement*;
sections: section+ EOF;
// section: SectionName /* statement_list */ End;
// Lexer Rules
fragment DIGIT: [0-9];
Integer: DIGIT+;
Float:
[+-]? (DIGIT+ ([.]DIGIT*)? | [.]DIGIT+) ([Ee][+-]? DIGIT+)?;
section:
'Header' statement_list End # headerSection
| 'Simulation' statement_list End # simulatorSection
| 'Constants' statement_list End # constantsSection
| 'Body' Integer statement_list End # bodySection
| 'Material' Integer statement_list End # materialSection
| 'Body Force' Integer statement_list End # bodyForceSection
| 'Equation' Integer statement_list End # equationSection
| 'Solver' Integer statement_list End # solverSection
| 'Boundary Condition' Integer statement_list End # boundaryConditionSection
| 'Initial Condition' Integer statement_list End # initialConditionSection
| 'Component' Integer statement_list End # componentSection;
End: 'End';
// statementEnd: ';' NewLine*;
NewLine: ('\r'? '\n' | '\n' | '\r') -> skip;
LineJoining:
'\\' WhiteSpace? ('\r'? '\n' | '\r' | '\f') -> skip;
WhiteSpace: [ \t\r\n]+ -> skip;
LineComment: '#' ~( '\r' | '\n')* -> skip;
EndOfStatment: ';' | NewLine;
Simulation
End
Constants
Equation 1
End
➜ grun ElmerSolver sections -tree -tokens < examples/ex001.sif
[@0,0:9='Simulation',<'Simulation'>,1:0]
[@1,11:13='End',<'End'>,2:0]
[@2,16:24='Constants',<'Constants'>,4:0]
[@3,28:35='Equation',<'Equation'>,6:0]
[@4,37:37='1',<Integer>,6:9]
[@5,39:41='End',<'End'>,7:0]
[@6,42:41='<EOF>',<EOF>,7:3]
line 6:0 missing 'End' at 'Equation'
(sections (section Simulation statement_list End) (section Constants statement_list <missing 'End'>) (section Equation 1 statement_list End) <EOF>)