antlr

Practical difference between parser rules and lexer rules in ANTLR?

非 Y 不嫁゛ 提交于 2019-11-26 13:22:17
I understand the theory behind separating parser rules and lexer rules in theory, but what are the practical differences between these two statements in ANTLR: my_rule: ... ; MY_RULE: ... ; Do they result in different AST trees? Different performance? Potential ambiguities? Jen wrote : ... what are the practical differences between these two statements in ANTLR ... MY_RULE will be used to tokenize your input source. It represents a fundamental building block of your language. my_rule is called from the parser, it consists of zero or more other parser rules or tokens produced by the lexer. That

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

风流意气都作罢 提交于 2019-11-26 09:44:42
问题 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

How to convert a String to its equivalent LINQ Expression Tree?

送分小仙女□ 提交于 2019-11-26 09:26:11
This is a simplified version of the original problem. I have a class called Person: public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } ...and lets say an instance: var bob = new Person { Name = "Bob", Age = 30, Weight = 213, FavouriteDay = '1/1/2000' } I would like to write the following as a string in my favourite text editor.... (Person.Age > 3 AND Person.Weight > 50) OR Person.Age < 3 I would like to take this string and my object instance and evaluate a TRUE or FALSE - i.e.

What does “fragment” mean in ANTLR?

只愿长相守 提交于 2019-11-26 07:32:21
问题 What does fragment mean in ANTLR? I\'ve seen both rules: fragment DIGIT : \'0\'..\'9\'; and DIGIT : \'0\'..\'9\'; What is the difference? 回答1: 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'..

Using ANTLR 3.3?

為{幸葍}努か 提交于 2019-11-26 06:55:14
问题 I\'m trying to get started with ANTLR and C# but I\'m finding it extraordinarily difficult due to the lack of documentation/tutorials. I\'ve found a couple half-hearted tutorials for older versions, but it seems there have been some major changes to the API since. Can anyone give me a simple example of how to create a grammar and use it in a short program? I\'ve finally managed to get my grammar file compiling into a lexer and parser, and I can get those compiled and running in Visual Studio

How does the ANTLR lexer disambiguate its rules (or why does my parser produce “mismatched input” errors)?

旧巷老猫 提交于 2019-11-26 06:48:05
问题 Note: This is a self-answered question that aims to provide a reference about one of the most common mistakes made by ANTLR users. When I test this very simple grammar: grammar KeyValues; keyValueList: keyValue*; keyValue: key=IDENTIFIER \'=\' value=INTEGER \';\'; IDENTIFIER: [A-Za-z0-9]+; INTEGER: [0-9]+; WS: [ \\t\\r\\n]+ -> skip; With the following input: foo = 42; I end up with the following run-time error: line 1:6 mismatched input \'42\' expecting INTEGER line 1:8 mismatched input \';\'

If/else statements in ANTLR using listeners

断了今生、忘了曾经 提交于 2019-11-26 06:18:00
I'm creating a simple programming language for a school project. I'm using ANTLR 4 to generate a lexer and a parser from my grammar. Until now, I have been using ANTLRs listener pattern to apply the actual functionality of the programming language. Now I would like to implement if/else statements but I'm not sure that these can actually be implemented when using the listener pattern as ANTLR decides in which order to traverse the parse tree when using listeners and I imagine that the implementation of if/else statements will require jumping around the parse tree depending on which condition in

When is EOF needed in ANTLR 4?

我是研究僧i 提交于 2019-11-26 05:39:41
问题 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? 回答1: 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

Practical difference between parser rules and lexer rules in ANTLR?

落爺英雄遲暮 提交于 2019-11-26 03:34:28
问题 I understand the theory behind separating parser rules and lexer rules in theory, but what are the practical differences between these two statements in ANTLR: my_rule: ... ; MY_RULE: ... ; Do they result in different AST trees? Different performance? Potential ambiguities? 回答1: Jen wrote : ... what are the practical differences between these two statements in ANTLR ... MY_RULE will be used to tokenize your input source. It represents a fundamental building block of your language. my_rule is

How to convert a String to its equivalent LINQ Expression Tree?

ぐ巨炮叔叔 提交于 2019-11-26 03:27:27
问题 This is a simplified version of the original problem. I have a class called Person: public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } ...and lets say an instance: var bob = new Person { Name = \"Bob\", Age = 30, Weight = 213, FavouriteDay = \'1/1/2000\' } I would like to write the following as a string in my favourite text editor.... (Person.Age > 3 AND Person.Weight > 50) OR Person