antlr

How do I manage optional whitespace in ANTLR?

扶醉桌前 提交于 2019-12-23 07:43:00
问题 I am trying to parse a data file in ANTLR - it has optional whitespace exemplified by 3 6 97 12 15 18 The following shows where the line starts and ends are. There is a newline at the end and there are no tabs. ^ 3 6$ ^ 97 12$ ^ 15 18$ ^ My grammar is: lines : line+; line : ws1 {System.out.println("WSOPT :"+$ws1.text+":");} num1 {System.out.println("NUM1 "+$num1.text);} ws2 {System.out.println("WS :"+$ws2.text+":");} num2 {System.out.println("NUM2 "+$num2.text);} NEWLINE ; num1 : INT ; num2 :

ANTLR java test file can't create object of tree grammar

爷,独闯天下 提交于 2019-12-23 04:34:45
问题 I am creating a parser using ANTLR 3.x that targets java. I have written both parser grammar (for creating Abstract Syntax Tree, AST) and Tree Grammar (for performing operations on AST). Finally, to test both grammar files, I have written a test file in Java. Have a look at the below code, protocol grammar grammar protocol; options { language = Java; output = AST; } tokens{ //imaginary tokens PROT; INITIALP; PROC; TRANSITIONS; } @header { import twoprocess.Configuration; package com.javadude

ANTLR java test file can't create object of tree grammar

你离开我真会死。 提交于 2019-12-23 04:34:10
问题 I am creating a parser using ANTLR 3.x that targets java. I have written both parser grammar (for creating Abstract Syntax Tree, AST) and Tree Grammar (for performing operations on AST). Finally, to test both grammar files, I have written a test file in Java. Have a look at the below code, protocol grammar grammar protocol; options { language = Java; output = AST; } tokens{ //imaginary tokens PROT; INITIALP; PROC; TRANSITIONS; } @header { import twoprocess.Configuration; package com.javadude

Catching errors in ANTLR and finding parent

主宰稳场 提交于 2019-12-23 04:04:41
问题 I have found out that I can catch errors during parsing by overwriting displayRecognitionError, but how do I find the parent "node" of this error? ex. if I have the grammar: prog: stat expr; stat: STRING; expr: INTEGER; And give it the input "abc def". Then I will get an error at "def" which should be an integer. At this point I then want to get the parent which is "expr" (since it fails inside the INTEGER part) and it's parent "prog". Kind of like printing stack trace in java. I tried to

Catching errors in ANTLR and finding parent

╄→尐↘猪︶ㄣ 提交于 2019-12-23 04:04:04
问题 I have found out that I can catch errors during parsing by overwriting displayRecognitionError, but how do I find the parent "node" of this error? ex. if I have the grammar: prog: stat expr; stat: STRING; expr: INTEGER; And give it the input "abc def". Then I will get an error at "def" which should be an integer. At this point I then want to get the parent which is "expr" (since it fails inside the INTEGER part) and it's parent "prog". Kind of like printing stack trace in java. I tried to

Antlr parser for and/or logic - how to get expressions between logic operators?

南笙酒味 提交于 2019-12-23 03:55:58
问题 I am using ANTLR to create an and/or parser+evaluator. Expressions will have the format like: x eq 1 && y eq 10 (x lt 10 && x gt 1) OR x eq -1 I was reading this post on logic expressions in ANTLR Looking for advice on project. Parsing logical expression and I found the grammar posted there a good start: grammar Logic; parse : expression EOF ; expression : implication ; implication : or ('->' or)* ; or : and ('&&' and)* ; and : not ('||' not)* ; not : '~' atom | atom ; atom : ID | '('

ANTLR grammar postfix

雨燕双飞 提交于 2019-12-23 03:37:34
问题 I was writing a simple grammar for a small language when I came across an issue. It's throwing a NullPointerException when I try to interpret it. Oh, and this is ANTLRWorks 3. Also - I specified that the language should be Python, but it still does stuff in Java :( Why? Here's the input: program Test1 = const t1 : int := 1; const t2 : int := 2; var x, y, z : int; begin x := 41; y := 10; z := 2 4 *; end Test1. Here's my code: grammar lang; options { language=Python; output=AST; ASTLabelType

java.lang.ClassNotFoundException: antlr.ANTLRException

末鹿安然 提交于 2019-12-23 02:55:53
问题 I have a method to get first result of a table. public T LoadFirstData() { T object = null; try { startOperation(); Query query = session.createQuery("from " + type.getName()); object = (T) query.list().get(0); } catch (HibernateException e) { HandleException(e); } finally { HibernateUtil.Close(session); } return object; } But, when I run the method, I have the exception Exception in thread "main" java.lang.NoClassDefFoundError: antlr/ANTLRException at org.hibernate.hql.ast

ANTLR4 - How do I get the token TYPE as the token text in ANTLR?

你离开我真会死。 提交于 2019-12-23 02:44:08
问题 Say I have a grammar that has tokens like this: AND : 'AND' | 'and' | '&&' | '&'; OR : 'OR' | 'or' | '||' | '|' ; NOT : 'NOT' | 'not' | '~' | '!'; When I visualize the ParseTree using TreeViewer or print the tree using tree.toStringTree(), each node's text is the same as what was matched. So if I parse "A and B or C", the two binary operators will be "and" / "or". If I parse "A && B || C", they'll be "&&" / "||". What I would LIKE is for them to always be "AND" / "OR / "NOT", regardless of

How can I hide parens in ANTLR4?

大兔子大兔子 提交于 2019-12-23 02:35:15
问题 For example, input = '(1+2)*3' . tree is like that '(expr (expr ((expr (expr 1) + (expr 2)) ))*(expr 3))' And then, I would like to hide or delete the '(' and ')' in the tree , they are no needed any more. I try to make it , but didn't. expr : ID LPAREN exprList? RPAREN | '-' expr | '!' expr | expr op=('*'|'/') expr | expr op=('+'|'-') expr | ID | INT | LPAREN expr RPAREN //### Parens Here #### ; LPAREN : '(' ; RPAREN : ')' ; What I want is** NOT** the following. PAREN : ( '(' | ')' ) ->