antlr

How do I get an Antlr Parser rule to read from both default AND hidden channel

只谈情不闲聊 提交于 2019-11-26 23:17:22
问题 I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding. Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning. Ex. this is the WhiteSpace lexer rule WS : ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;} ; And this is my rule where I would like to include whitespace raw

ANTLR4 visitor pattern on simple arithmetic example

左心房为你撑大大i 提交于 2019-11-26 22:53:47
问题 I am a complete ANTLR4 newbie, so please forgive my ignorance. I ran into this presentation where a very simple arithmetic expression grammar is defined. It looks like: grammar Expressions; start : expr ; expr : left=expr op=('*'|'/') right=expr #opExpr | left=expr op=('+'|'-') right=expr #opExpr | atom=INT #atomExpr ; INT : ('0'..'9')+ ; WS : [ \t\r\n]+ -> skip ; Which is great because it will generate a very simple binary tree that can be traversed using the visitor pattern as explained in

In ANTLR, is there a shortcut notation for expressing alternation of all the permutations of some set of rules?

柔情痞子 提交于 2019-11-26 22:26:20
问题 In ANTLR I want to define a rule like this: rule : ( a b c | a c b | b a c | b c a | c a b | c b a ); But in my case I have 10 rules instead of three, that I want to permute so it gets very impractical. Is there any way of expressing this in ANTLR without having to write all the permutations? 回答1: I would just match any a , b or c once or more: rule : ( a | b | c )+ ; and then, after parsing, traversing the parse tree and checking if a , b and c all matched exactly once. But Yes, it is

antlr3 - Generating a Parse Tree

我与影子孤独终老i 提交于 2019-11-26 21:25:58
问题 I'm having trouble figuring out the antlr3 API so I can generate and use a parse tree in some javascript code. When I open the grammar file using antlrWorks (their IDE), the interpreter is able to show me the parse tree, and it's even correct. I'm having a lot of difficulties tracking down resources on how to get this parse tree in my code using the antlr3 runtime. I've been messing around with the various functions in the runtime and Parser files but to no avail: var input = "(PR=5000)",

How to specify a target package for ANTLR?

十年热恋 提交于 2019-11-26 20:51:39
问题 If I call: java org.antlr.Tool -o outdir sources/com/example/Java5.g ...with antlr-3.1.3 the parser and lexer code will be generated in the directory outdir/sources/com/example . But the generated classes don't have any package statement. I need them to life in the package com.example . Is there a way to specify the target package? 回答1: ANTLR provides a header tool which allows you to include package and imports. You include this in your *.g grammar file: @header { package org.xmlcml.cml

What does “fragment” mean in ANTLR?

谁说胖子不能爱 提交于 2019-11-26 20:14:39
What does fragment mean in ANTLR? I've seen both rules: fragment DIGIT : '0'..'9'; and DIGIT : '0'..'9'; What is the difference? A fragment is somewhat akin to an inline function: It makes the grammar more readable and easier to maintain. A fragment will never be counted as a token, it only serves to simplify a grammar. Consider: NUMBER: DIGITS | OCTAL_DIGITS | HEX_DIGITS; fragment DIGITS: '1'..'9' '0'..'9'*; fragment OCTAL_DIGITS: '0' '0'..'7'+; fragment HEX_DIGITS: '0x' ('0'..'9' | 'a'..'f' | 'A'..'F')+; In this example, matching a NUMBER will always return a NUMBER to the lexer, regardless

ANTLR 4.5 - Mismatched Input 'x' expecting 'x'

佐手、 提交于 2019-11-26 18:02:10
问题 I have been starting to use ANTLR and have noticed that it is pretty fickle with its lexer rules. An extremely frustrating example is the following: grammar output; test: FILEPATH NEWLINE TITLE ; FILEPATH: ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|' '|'-'|'_'|'.')+ ; NEWLINE: '\r'? '\n' ; TITLE: ('A'..'Z'|'a'..'z'|' ')+ ; This grammar will not match something like: c:\test.txt x Oddly if I change TITLE to be TITLE: 'x' ; it still fails this time giving an error message saying "mismatched input

When is EOF needed in ANTLR 4?

家住魔仙堡 提交于 2019-11-26 17:54:12
The TestDriver in ANTLRWorks2 seems kind of finicky about when it'll accept a grammer without and explicit EOF and when it will not. The Hello grammar in the ANTLR4 Getting Started Guide doesn't use EOF anywhere, so I inferred that it's better to avoid explicit EOF if possible. What is the best practice for using EOF ? When do you actually need it? You should include an explicit EOF at the end of your entry rule any time you are trying to parse an entire input file. If you do not include the EOF , it means you are not trying to parse the entire input, and it's acceptable to parse only a

ANTLR What is simpliest way to realize python like indent-depending grammar?

不羁岁月 提交于 2019-11-26 17:39:14
问题 I am trying realize python like indent-depending grammar. Source example: ABC QWE CDE EFG EFG CDE ABC QWE ZXC As i see, what i need is to realize two tokens INDENT and DEDENT, so i could write something like: grammar mygrammar; text: (ID | block)+; block: INDENT (ID|block)+ DEDENT; INDENT: ????; DEDENT: ????; Is there any simple way to realize this using ANTLR? (I'd prefer, if it's possible, to use standard ANTLR lexer.) 回答1: I don't know what the easiest way to handle it is, but the

Allow Whitespace sections ANTLR4

只谈情不闲聊 提交于 2019-11-26 14:33:23
问题 I have an antlr4 grammar designed for an a domain specific language that is embedded into a text template. There are two modes: Text (whitespace should be preserved) Code (whitespace should be ignored) Sample grammar part: template : '{' templateBody '}' ; templateBody : templateChunk* ; templateChunk : code # codeChunk // dsl code, ignore whitespace | text # textChunk // any text, preserve whitespace ; The rule for code may contain a nested reference to the template rule. So the parser must