antlr

Ambiguous call expression in ANTLR4 grammar

耗尽温柔 提交于 2019-11-27 08:25:52
问题 I have a simple grammar (for demonstration) grammar Test; program : expression* EOF ; expression : Identifier | expression '(' expression? ')' | '(' expression ')' ; Identifier : [a-zA-Z_] [a-zA-Z_0-9?]* ; WS : [ \r\t\n]+ -> channel(HIDDEN) ; Obviously the second and third alternatives in the expression rule are ambiguous. I want to resolve this ambiguity by permitting the second alternative only if an expression is immediately followed by a '(' . So the following bar(foo) should match the

ANTLR 4 Parser Grammar

巧了我就是萌 提交于 2019-11-27 08:05:07
问题 How can I improve my parser grammar so that instead of creating an AST that contains couple of decFunc rules for my testing code. It will create only one and sum becomes the second root. I tried to solve this problem using multiple different ways but I always get a left recursive error. This is my testing code : f :: [Int] -> [Int] -> [Int] f x y = zipWith (sum) x y sum :: [Int] -> [Int] sum a = foldr(+) a This is my grammar: This is the image that has two decFunc in this link http://postimg

ANTLR not throwing errors on invalid input

≯℡__Kan透↙ 提交于 2019-11-27 07:36:05
问题 I'm using ANTLR to parse logical expressions in a Java tool I'm writing, and I'm having issues because passing invalid input strings to the generated ANTLR lexer and parser doesn't cause any exceptions. Instead of throwing a RecognitionException, like I would expect, the generated files just print the error message to the console and return as if no errors occurred, causing my program to crash when it runs into the empty data later. I used ANTLRWorks version 1.4.3 to generate the files, and

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

给你一囗甜甜゛ 提交于 2019-11-27 07:27:47
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.) I don't know what the easiest way to handle it is, but the following is a relatively easy way. Whenever you match a line break in your lexer, optionally match one or more

ANTLR4: Using non-ASCII characters in token rules

放肆的年华 提交于 2019-11-27 06:53:12
问题 On page 74 of the ANTRL4 book it says that any Unicode character can be used in a grammar simply by specifying its codepoint in this manner: '\uxxxx' where xxxx is the hexadecimal value for the Unicode codepoint. So I used that technique in a token rule for an ID token: grammar ID; id : ID EOF ; ID : ('a' .. 'z' | 'A' .. 'Z' | '\u0100' .. '\u017E')+ ; WS : [ \t\r\n]+ -> skip ; When I tried to parse this input: Gŭnter ANTLR throws an error, saying that it does not recognize ŭ . (The ŭ

Build symbol table from grammar [closed]

ⅰ亾dé卋堺 提交于 2019-11-27 05:37:23
I am trying to build a symbol table from my grammar (done with antlr) by using eclipse. However I don't know by what to begin. I think I read somewhere that you would need the parser and lexer generated by antlr to do that. Does someone know an easy example so that I can understand how it works please ? A symbol table is just a versioned map of id's to values. This is one solution, using a push and pop of scopes as the versioning mechanism -- push a scope on entry of a scope defining rule and pop on exit. package net.certiv.metal.symbol; import java.util.ArrayList; import java.util

ANTLR 4 tree inject/rewrite operator

空扰寡人 提交于 2019-11-27 04:26:17
问题 In ANTLR 3 you could just do the following: andExpression : (andnotExpression -> andnotExpression) (AND? a=andnotExpression -> ^(AndNode $andExpression $a))* ; Any idea how to do it in the new version? 回答1: As mentioned by Sam (280Z28), ANTLR 4 does not have rewrite operators. When generating the parser, ANTLR 4 creates some listener classes that you can use to listen for "enter" and "exit" events of all parser rules. Also, ANTLR 4 supports "direct left recursive rules", so your expression

Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)

我的梦境 提交于 2019-11-27 01:51:38
问题 I'm trying to install the ANTLR 4 IDE on Eclipse Luna (4.4). I've installed it from the Marketplace but I have no idea how to create a project that has an ANTLR 4 Lexer/Parser in it. When I go to create a new project I don't see any options for ANTLR 4. I tried creating a .g4 file and it opens in the editor but when I save it doesn't do anything. 回答1: I looked around all over the internet and found a handful of resources that I cobbled together and found a solution by trial and error. Below

Visualizing an AST created with ANTLR (in a .Net environment)

人走茶凉 提交于 2019-11-27 01:38:21
For a pet project I started to fiddle with ANTLR. After following some tutorials I'm now trying to create the grammar for my very own language and to generate an AST. For now I'm messing around in ANTLRWorks mostly, but now that I have validated that the parse tree seems to be fine I'd like to (iteratively, because I'm still learning and still need to make some decisions regarding the final structure of the tree) create the AST. It seems that antlrworks won't visualize it (or at least not using the "Interpreter" feature, Debug's not working on any of my machines). Bottom line: Is the only way

Antlr与Regex

那年仲夏 提交于 2019-11-27 01:26:08
Antlr与Regex都是文本分析工具。 Antlr内部分为词法(Lexer)和语法(Parser),在Antlr中,变量第一个字符大写表示词法,变量第一个字符小写表示语法。词法表示哪些是有效的词,语法表示有效的词怎么样组合才是有效的,延伸一点就是语义,语义表示描述的内容是否正确,涉及到人工智能。Antlr明确表明了哪些是词法,哪些是语法。 Regex好像把词法语法合在了一起,可以认为每一个小块是一个词法,这些小块的顺序组合是语法。 转载于:https://www.cnblogs.com/nzbbody/p/3365472.html 来源: https://blog.csdn.net/weixin_30716725/article/details/99234449