antlr4

How does ANTLR decide which lexer rule to apply? The longest matching lexer rule wins?

孤街醉人 提交于 2019-12-08 04:32:09
问题 The input content: The grammar: grammar test; p : EOF; Char : [a-z]; fragment Tab : '\t'; fragment Space : ' '; T1 : (Tab|Space)+ ->skip; T2 : '#' T1+ Char+; The matching result is this: [@0,0:6='# abc',<T2>,1:0] <<<<<<<< PLACE 1 [@1,7:6='<EOF>',<EOF>,1:7] line 1:0 extraneous input '# abc' expecting <EOF> Please ignore the error in the last line. I am wondering why the token matched at PLACE 1 is T2 . In the grammar file, the T2 lexer rule goes after the T1 lexer rule. So I expect T1 rule

How to detect beginning of line, or: “The name 'getCharPositionInLine' does not exist in the current context”

感情迁移 提交于 2019-12-08 00:46:12
问题 I'm trying to create a Beginning-Of-Line token: lexer grammar ScriptLexer; BOL : {getCharPositionInLine() == 0;}; // Beginning Of Line token But the above emits the error The name 'getCharPositionInLine' does not exist in the current context As it creates this code: private void BOL_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 0: getCharPositionInLine() == 0; break; } } Where the getCharPositionInLine() method doesn't exist... 回答1: Simplest approach is to just

Antlr4-JS actions in grammar: how to access tokens and define funcs ? (direct in java, not in JS)

痞子三分冷 提交于 2019-12-07 23:08:16
问题 I'm trying to adapt to JS target the Expr.g4 of the book. In this example, actions are directly in the grammar. They include utility functions defined in @parser::members , that are called in the rules. The example works well in Java, but in its JS translation I have 2 problem: - getting the action function visible by the action rule - getting the tokens recognized in the function. I finally manage to get this working formulation: @parser::members { myeval = function(left, op, right) { switch

What is the replacement for $type attribute in ANTLR 4?

我怕爱的太早我们不能终老 提交于 2019-12-07 13:57:42
问题 Here is the example. This ($type) is not recognised by ANTLR4. Number //options { backtrack=true; } : IntegerLiteral { $type = IntegerLiteral; } | FloatLiteral { $type = FloatLiteral; } | IntegerLiteral { $type = IntegerLiteral; } ; What could this be replaced by? Thank you. 回答1: In ANTLR v4, do: Number : IntegerLiteral {setType(IntegerLiteral);} | ... 回答2: In ANTLR 4, this is the new syntax: Foo : Bar -> type(SomeType) | ... ; However, for the rule you have above you should just remove the

how print parse-tree using python2 runtime with antlr4

百般思念 提交于 2019-12-07 07:13:14
问题 I'm trying to use antlr4 version 4.4 and the python2 runtime. The grammar is from the antlr4 book, page 6, file: Hello.g4 : grammar Hello; r : 'hello' ID ; ID : [a-z]+ ; WS : [ \t\r\n]+ -> skip ; and I generate lexer and parser with command antlr4 -Dlanguage=Python2 Hello.g4 the files HelloLexer.py , HelloParser.py and HelloListener.py among other, are then generated. I make a main program test.py to test the generated python parser: from antlr4 import * from HelloLexer import HelloLexer from

Antlr Error Strategy to skip tokens until rule matches again

大憨熊 提交于 2019-12-07 07:01:23
问题 I tried this solution but it didn't seem to work for me Here's an excerpt from my grammer: module : BEGIN MODULE IDENT STRING module_element* END MODULE ; module_element : element_1 | element_2 | element_3 | ... ; There is a bigger tree below each element. Now when a RecognitionException occurs I want to consume tokens until either the next module_element matches or the parent END MODULE matches. Any hints on how to do this inside a class inheriting from DefaultErrorStrategy? edit: Here is a

Rule variables in ANTLR4

一笑奈何 提交于 2019-12-07 05:30:28
问题 I'm trying to convert my grammar from v3 to v4 and having some trouble. In v3 I have rules like this: dataspec[DataLayout layout] returns [DataExtractor extractor] @init { DataExtractorBuilder builder = new DataExtractorBuilder(layout); } @after { extractor = builder.create(); } : first=expr { builder.addAll(first); } (COMMA next=expr { builder.addAll(next); })* ; expr returns [List<ValueExtractor> ext] ... However, with rules in v4 returning these custom context objects instead of what I

ANTLR4 Parser, Visitor not created

一笑奈何 提交于 2019-12-07 05:06:35
问题 I'm new to ANTLR and trying to write grammar in ANTLR4 without any prior brush with the previous version. I'm following the book ' The Definitive ANTLR 4 Reference '. I use Eclipse and installed ANTLR4 IDE as given in here. I wrote the following grammar in Expr.g4: grammar Expr; import Common; options{ language = Java; } prog: stat+; stat: expr NEWLINE | ID '=' expr NEWLINE | NEWLINE; expr: expr ('/'|'*') expr | expr ('+'|'-') expr | INT | ID | '('expr')'; The Common.g4 contains the following

How to avoid mutual left-recursion in ANTLR 4

血红的双手。 提交于 2019-12-07 04:50:01
问题 I am writing a grammar to handle scalar and vector expressions. The grammar below is simplified to show the problem I have where a scalar expression can be derived from a vector and a vector can be derived from a scalar. For example, a vector could be a literal [1, 2, 3] or the product of a scalar and a vector 2 * [1, 2, 3] (equivalent to [2, 4, 6] ). A scalar could be a literal 2 or an index into a vector [1, 2, 3][1] (equivalent to 2 ). grammar LeftRecursion; Integer : [0-9]+ ; WhiteSpace :

ANTLR doesn't give correct output tokens for Scala Grammar

无人久伴 提交于 2019-12-07 00:31:47
问题 I am new to Scala and I am trying to parse Scala files with the use of Scala Grammar and ANTLR. Below is the code for Scala Grammar which I got from the git hub link: https://github.com/antlr/grammars-v4/tree/master/scala There are chances of repo to be moved so I am pasting the Scala grammar code here: grammar Scala; literal : '-'? IntegerLiteral | '-'? FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | SymbolLiteral | 'null' ; qualId : Id ('.' Id)* ; ids : Id (',' Id