antlr4

How can I check if first character of a line is “*” in ANTLR4?

眉间皱痕 提交于 2019-12-10 23:03:26
问题 I am trying to write a parser for a relatively simple but idiosyncratic language. Simply put, one of the rules is that comment lines are denoted by an asterisk only if that asterisk is the first character of the line. How might I go about formalising such a rule in ANTLR4? I thought about using: START_LINE_COMMENT: '\n*' .*? '\n' -> skip; But I am certain this won't work with more than one line comment in a row, as the newline at the end will be consumed as part of the START_LINE_COMMENT

Recognize multiple line comments within a single line with ANTLR4

北城余情 提交于 2019-12-10 21:52:04
问题 I want to parse PostScript code with ANTLR4. I finished with the grammar, but one particular language extension (which was introduced by someone else) makes trouble being reconized. A short example: 1: % This is a line comment 2: % The next line just pushes the value 10 onto the stack 3: 10 4: 5: %?description This is the special line-comment in question 6: /procedure { 7: /var1 30 def %This just creates a variable 8: /var2 10 def %?description A description associated with var2 %?default 20

How to preserve whitespace when we use text attribute in Antlr4

ε祈祈猫儿з 提交于 2019-12-10 17:30:06
问题 I want to keep white space when I call text attribute of token, is there any way to do it? Here is the situation: We have the following code IF L > 40 THEN; ELSE IF A = 20 THEN PUT "HELLO"; In this case, I want to transform it into: if (!(L>40){ if (A=20) put "hello"; } The rule in Antlr is that: stmt_if_block: IF expr THEN x=stmt (ELSE y=stmt)? { if ($x.text.equalsIgnoreCase(";")) { WriteLn("if(!(" + $expr.text +")){"); WriteLn($stmt.text); Writeln("}"); } } But the result looks like: if(!(L

Controlling false IntelliJ code editor error in Scala plugin

徘徊边缘 提交于 2019-12-10 17:26:06
问题 I have Java code generated from ANTLR4. Scala is using the Java code by extending some of the methods. The issue is that IntelliJ's scala plugin does not seem to know the relationship between Java base class and Scala subclass to show a false positive error message; it reports "Method ... overrides nothing" when Scala overrides Java method. How to control the error level in IntelliJ to suppress this error message? 回答1: Most of the false-negatives produced by Scala plugin are caused by type

What happened to options in rules in ANTLR 4?

我的未来我决定 提交于 2019-12-10 16:14:54
问题 This does not compile in ANTLR 4: Number options { backtrack=true; } : (IntegerLiteral Range)=> IntegerLiteral { $type = IntegerLiteral; } | (FloatLiteral)=> FloatLiteral { $type = FloatLiteral; } | IntegerLiteral { $type = IntegerLiteral; } ; because of backtrace= true... What happened to it? WHat should I use in ANTLR 4 instread of it? 回答1: At the moment, there are no rule-level options in ANTLR v4. Note that backtrack=true is no longer needed since the new parsing algorithm has no need for

Token recognition error: antlr

强颜欢笑 提交于 2019-12-10 15:25:39
问题 I have an ANTLR 4 grammar: grammar Test; start : NonZeroDigit '.' Digit Digit? EOF ; DOT : '.' ; PLUS : '+' ; MINUS : '-' ; COLON : ':' ; COMMA : ',' ; QUOTE : '\"' ; EQUALS : '=' ; SEMICOLON : ';' ; UNDERLINE : '_' ; BACKSLASH : '\\' ; SINGLEQUOTE : '\'' ; RESULT_TYPE_NONE : 'NONE' ; RESULT_TYPE_RESULT : 'RESULT' ; RESULT_TYPE_RESULT_SET : 'RESULT_SET' ; TYPE_INT : 'Int' ; TYPE_LONG : 'Long' ; TYPE_BOOL : 'Bool' ; TYPE_DATE : 'Date' ; TYPE_DOUBLE : 'Double' ; TYPE_STRING : 'String' ; TYPE

Is there a parser equivalent of 'fragment' marking in ANTLR4?

风流意气都作罢 提交于 2019-12-10 15:12:19
问题 Is there a way to tell ANTLR4 to inline the parser rule? It seems reasonable to have such feature. After reading the book on ANTLR ( "The Definitive ANTLR 4 Reference" ) I haven't found such possibility, but changes might've been introduced in the 4 years since the book was released, so I guess it is better to ask here. Consider the following piece of grammar: file: ( item | class_decl )*; class_decl: 'class' class_name '{' type_decl* data_decl* code_decl* '}'; type_decl: 'typedef' ('bool'|

How do I diagnose ambiguities in my ANTLR4 grammar?

北慕城南 提交于 2019-12-10 14:49:30
问题 Short version: I am using parser.addErrorListener(new DiagnosticErrorListener()); parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); But when I run my parser I don't see any output to the affect of 'reportAmbiguity ...' as the ANTLR4 book shows. How can I see ambiguities in my grammar? Long version: The parsing speed I currently get with ANTLR4 is around 90kb/s on a 8-core 2.67ghz Xeon E5640 machine. The lexing speed is about 5mb/s, so that is fine. While

ANTLR mismatched input '<EOF>'

这一生的挚爱 提交于 2019-12-10 14:40:37
问题 Given the following ANTLR 4.1 grammar, with one line intentionally commented out ... grammar Foobar; //whyDoesThisRuleHelp : expression ; expression : operand | binaryOperation ; binaryOperation : operand WS BINARY_OPERATOR WS expression ; operand : LETTER ; BINARY_OPERATOR : 'EQ' ; LETTER : [a-z] ; WS : [ \n]+ ; .. why does echo -n "a EQ b" | grun Foobar expression produce line 1:6 mismatched input '<EOF>' expecting WS .. but if we uncomment the block : expression ; line above then grun

Writing parser rules sensitive to whitespace while skipping WS from the lexer

本秂侑毒 提交于 2019-12-10 14:14:38
问题 I am having some troubles in handling whitespace. In the following excerpt of a grammar, I set up the lexer so that the parser skips whitespace: ENTITY_VAR : 'user' | 'resource' ; INT : DIGIT+ | '-' DIGIT+ ; ID : LETTER (LETTER | DIGIT | SPECIAL)* ; ENTITY_ID : '__' ENTITY_VAR ('_w_' ID)?; NEWLINE : '\r'? '\n'; WS : [ \t\r\n]+ -> skip; // skip spaces, tabs, newlines fragment LETTER : [a-zA-Z]; fragment DIGIT : [0-9]; fragment SPECIAL : ('_' | '#' ); The problem is, I would like to match