I am using antlr 4 and I want to get the tree produce by antlr 4 in prefix represention ... the tree produce by antlr 4 is infix represtion But I wanted in prefix represtion ... so How I can do it ?
Related
Developing a new grammar with ANTLR. My grammar supports basic math and boolean expressions like "4 equals (2 minuses 2)" or "true", "false". All operators are in natural language. I want to support other languages in their nature. For example, "4 equals 4" is "4 ist 4" in German.
What is the best practice to localize tokens and/or expressions?
In our project we follow this structure. There are files FooLexerBase.g and FooLexerLang1.g, FooLexerLang2.g and so on. The base grammar defines common token rules. Tokens that depend on language are not defined in the base, but can be referred to. These tokens are defined in the language-specific grammars, that all also include the base.
So, basically it looks something like this:
FooLexerBase.g:
lexer grammar FooLexerBase;
...
FLOATING_POINT
: DIGIT+ EXPONENT
| DIGIT+ DECIMAL_SEP DIGIT* EXPONENT?
| DECIMAL_SEP DIGIT+ EXPONENT?;
...
DIGIT and EXPONENT are defined in the base, since they are common, while DECIMAL_SEP is language-specific.
For example, FooLexerGerman.g looks like this:
lexer grammar FooLexerGerman;
import base = FooBase;
...
fragment
DECIMAL_SEP: ',';
...
Finally, parser grammar is common for all languages. It is defined this way:
parser grammar FooParser;
options {
tokenVocab = FooLexerBase;
}
...
It is important to not process FooLexerBase with ANTLR, but pass all other grammars through it.
At runtime you build a parser and pass an appropriate lexer as argument to the constructor. I guess it looks more or less the same in any programming language (we use Java).
Given a set S of n rules, I need an antlr4 rule to match any of S subset, in any order :
each rule of S can appear zero or one time
any permutation of the subset is ok
Example :
Given S = {a,b}, (n = 2) the rule must match
a
b
a b
b a
while "a b b", for instance must not match.
It is possible to parse such expression with an antlr4 grammar ? My real set has n = 6, so listing all combinations in the grammar seems not to be a possible choice !
No, you can't define combinations and/or permutations of rules in ANTLR (or any other parser generator that I know).
You could use predicates to accomplish your goal, but that means adding target specific code to your grammar: I'd just parse any a or b and validate the structure after parsing (in a custom visitor/listener).
I'm using ANTLR4 to build AST tree, I download g4 file from: https://github.com/antlr/grammars-v4/tree/master/sqlite
Add the option in the head of g4 file:
options{
output=AST;
ASTLabelType=CommonTree;
language=Java;
}
but while compile g4 file, it output :
ANTLR Tool v4.6 (D:\antlr-4.6-complete.jar)
SQLite.g4 -o C:\Users\macro\workspace\tdsql\target\generated-sources\antlr4 -listener -no-visitor -encoding UTF-8
warning(83): SQLite.g4:34:4: unsupported option output
warning(83): SQLite.g4:35:4: unsupported option ASTLabelType
does antlr4 not support using ASTLabelType to build a AST tree? and how can I build a AST tree with antlr4?
I'm an Antlr newbie myself so there are better-qualified people who can answer this. That said, the AST output option was deprecated between Antlr3 and Antlr4. Antlr3 will generate an AST but Antlr4 won't.
Your alternatives in Antlr4 are to use the Listener pattern (to walk the parse tree) or the Visitor pattern (to visit & evaluate nodes). Either - or both - of those can be used after running the Lexer and Parser.
There are a number of examples that can be found with some searching. Here's one for the Visitor pattern. This page compares Listeners and Visitors.
I'm assuming this isn't possible, but I'm just wondering if you could convert an expression to a string.
For example, the following adds five and five together
print(tostring(5 + 5)) --> 10
I was wondering if you could do something along the lines of the following.
print(tostring(5 + 5)) --> 5 + 5
I'm wondering because I'm making a graphing calculator, and to create a function, it has to be a string. I was hoping I could make it more user-friendly by making it so you can just input an expression such as x ^ 2 instead of "x ^ 2"
What you want to do is convert an expression 5 + 5 to a string "5 + 5".
AFAIK, you can't do that in Lua. However, you can do the opposite by transforming "5 + 5" to 5 + 5 using loadstring. Hope it help.
If you have the expression as part of some Lua code, you can parse that code into something like Abstract Syntax Tree (AST) and then convert that AST into whatever structure you need, which may be a string or a function (this may be more convenient for your graphing calculator).
There are several Lua parsers that can do this (both converting Lua code into AST and generating Lua code from AST); both Metalua (5.1 only? bytecode generation only?) and TypedLua (5.2+) can do that and there are some other Lua parsers listed here.
I've got the equivalent of an AST that a user has built using a rule engine. But when displaying a list of the rules, I'd like to be able to "pretty print" each rule into something that looks nice**. Internally when represented as a string they look like s-expressions so imagine something like:
(and (contains "foo" "foobar") (equals 4 (plus 2 2 )))
Can anyone point me at a program that has done a good job of displaying rules in a readable fashion?
** Needs to be localizable too, but I guess we'll leave that for extra credit.
Maybe check out the Attempto project that is developing Attempto Controlled English (ACE). ACE allows you to write rules in a subset of English. For example:
If "foo" contains "foobar" and "foobar" does not contain "foo" then 4 = 2 + 2.
The ACE parser converts such rules into a logical form called Discourse Representation Structure (DRS). For the above example, it looks like this:
[]
[A]
predicate(A, contain, string(foo), string(foobar))-1
NOT
[B]
predicate(B, contain, string(foobar), string(foo))-1
=>
[]
formula(int(4), =, expr(+, int(2), int(2)))-1
There is a tool called DRS verbalizer that converts DRSs into ACE. For the above DRS you would get:
If "foo" contains "foobar" and it is false that "foobar" contains "foo" then 4 = ( 2 + 2 ).
In your case, you would have to convert your rule representation into the DRS (which should be quite straight-forward), and then you can directly use the DRS verbalizer. The mentioned tools are available under the LGPL license.