antlr

Interactive Antlr

不问归期 提交于 2019-11-29 11:38:23
I'm trying to write a simple interactive (using System.in as source) language using antlr, and I have a few problems with it. The examples I've found on the web are all using a per line cycle, e.g.: while(readline) result = parse(line) doStuff(result) But what if I'm writing something like pascal/smtp/etc, with a "first line" looks like X requirment? I know it can be checked in doStuff, but I think logically it is part of the syntax. Or what if a command is split into multiple lines? I can try while(readline) lines.add(line) try result = parse(lines) lines = [] doStuff(result) catch nop But

How do I make a TreeParser in ANTLR3?

醉酒当歌 提交于 2019-11-29 11:35:10
I'm attemping to learn language parsing for fun... I've created a ANTLR grammar which I believe will match a simple language I am hoping to implement. It will have the following syntax: <FunctionName> ( <OptionalArguments>+) { <OptionalChildFunctions>+ } Actual Example: ForEach(in:[1,2,3,4,5] as:"nextNumber") { Print(message:{nextNumber}) } I believe I have the grammar working correctly to match this construct, and now I am attemping to build an Abstract Syntax Tree for the language. Firstly, I must admit I'm not exactly sure HOW this tree should look. Secondly, I'm at a complete loss how to

How Get error messages of antlr parsing?

假如想象 提交于 2019-11-29 11:12:11
I wrote a grammar with antlr 4.4 like this : grammar CSV; file : row+ EOF ; row : value (Comma value)* (LineBreak | EOF) ; value : SimpleValueA | QuotedValue ; Comma : ',' ; LineBreak : '\r'? '\n' | '\r' ; SimpleValue : ~(',' | '\r' | '\n' | '"')+ ; QuotedValue : '"' ('""' | ~'"')* '"' ; then I use antlr 4.4 for generating parser & lexer, this process is successful after generate classes I wrote some java code for using grammar import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CommonTokenStream; public class Main { public static void main(String[] args) { String source

Why my antlr lexer java class is “code too large”?

冷暖自知 提交于 2019-11-29 10:50:45
This is the lexer in Antlr (sorry for a long file): lexer grammar SqlServerDialectLexer; /* T-SQL words */ AND: 'AND'; BIGINT: 'BIGINT'; BIT: 'BIT'; CASE: 'CASE'; CHAR: 'CHAR'; COUNT: 'COUNT'; CREATE: 'CREATE'; CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'; DATETIME: 'DATETIME'; DECLARE: 'DECLARE'; ELSE: 'ELSE'; END: 'END'; FLOAT: 'FLOAT'; FROM: 'FROM'; GO: 'GO'; IMAGE: 'IMAGE'; INNER: 'INNER'; INSERT: 'INSERT'; INT: 'INT'; INTO: 'INTO'; IS: 'IS'; JOIN: 'JOIN'; NOT: 'NOT'; NULL: 'NULL'; NUMERIC: 'NUMERIC'; NVARCHAR: 'NVARCHAR'; ON: 'ON'; OR: 'OR'; SELECT: 'SELECT'; SET: 'SET'; SMALLINT: 'SMALLINT';

Antlr rule priorities

只谈情不闲聊 提交于 2019-11-29 10:43:13
Firstly I know this grammar doesn't make sense but it was created to test out the ANTLR rule priority behaviour grammar test; options { output=AST; backtrack=true; memoize=true; } rule_list_in_order : ( first_rule | second_rule | any_left_over_tokens)+ ; first_rule : FIRST_TOKEN ; second_rule: FIRST_TOKEN NEW_LINE SECOND_TOKEN NEW_LINE; any_left_over_tokens : NEW_LINE | FIRST_TOKEN | SECOND_TOKEN; FIRST_TOKEN : 'First token here' ; SECOND_TOKEN : 'Second token here'; NEW_LINE : ('\r'?'\n') ; WS : (' '|'\t'|'\u000C') {$channel=HIDDEN;} ; When I give this grammar the input 'First token here

How to match any symbol in ANTLR parser (not lexer)?

孤者浪人 提交于 2019-11-29 10:37:54
How to match any symbol in ANTLR parser (not lexer)? Where is the complete language description for ANTLR4 parsers? UPDATE Is the answer is "impossible"? You first need to understand the roles of each part in parsing: The lexer: this is the object that tokenizes your input string. Tokenizing means to convert a stream of input characters to an abstract token symbol (usually just a number). The parser: this is the object that only works with tokens to determine the structure of a language. A language (written as one or more grammar files) defines the token combinations that are valid. As you can

ANTLR resolving non-LL(*) problems and syntactic predicates

纵然是瞬间 提交于 2019-11-29 08:50:13
consider following rules in the parser: expression : IDENTIFIER | (...) | procedure_call // e.g. (foo 1 2 3) | macro_use // e.g. (xyz (some datum)) ; procedure_call : '(' expression expression* ')' ; macro_use : '(' IDENTIFIER datum* ')' ; and // Note that any string that parses as an <expression> will also parse as a <datum>. datum : simple_datum | compound_datum ; simple_datum : BOOLEAN | NUMBER | CHARACTER | STRING | IDENTIFIER ; compound_datum : list | vector ; list : '(' (datum+ ( '.' datum)?)? ')' | ABBREV_PREFIX datum ; fragment ABBREV_PREFIX : ('\'' | '`' | ',' | ',@') ; vector : '#('

ANTLR and Android

只愿长相守 提交于 2019-11-29 08:48:41
is there any guide how to use ANTLR on Android? I have found some ANTLR portation for Android but it looks like being without any tutorial or manual. Do you know where to find some? (and yes, I have been googling...) Thx Bart Kiers After reading the README from this ANTLR port : AntlrJavaRuntime - Earlence Fernandes, The CRePE Project, VU Amsterdam ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The Runtime is available as an external library against which apps can link. It provides the necessary mechanisms to execute Lexer/Parser code generated by the ANTLR tool. The

build AST in antlr4

人盡茶涼 提交于 2019-11-29 07:51:32
I was wondering whether we could build an AST using Antlr version 4. I couldn't find any reference on building it using antlr4. One SO answer says that it would be easy to use antlr4 which produces only parse tree but my question is what about the efficiency ? It forces us to crawl whole parse tree instead of an abstract syntax tree which is not efficient way to walk through the whole tree and perform task using visitors. There are two key items I'd like to point out first: Efficiency covers more than simple speed of execution. It can also apply to memory overhead, maintainability, and the

Hibernate的Antlr在Weblogic中产生Jar冲突的历史缘故以及解决办法

我的未来我决定 提交于 2019-11-29 05:55:20
Hibernate使用的文法分析器是antlr,WebLogic同样也是。 不少用户碰到ClassNotFoundException: org.hibernate.hql.ast.HqlToken的典型问题,这个典型问题已经通过配置weblogic.xml,要求Web应用优先加载WEB-INF的Jar(即应用Classloader)而非WebLogic的System Classloader得以勉强解决: <weblogic-web-app> <container-descriptor> <prefer-web-inf-classes>true</prefer-web-inf-classes> </container-descriptor> </weblogic-web-app> Hibernate 3.0的用户发现上述的方法依然不能解决问题,因为Hibernate使用了Class.forName去Load一个类,而JVM加载类的方式是先 System Classloader(WebLogic),后Application Classloader(Web应用),于是,应用即使采用prefer-web-inf-classes策略,但Class.forName还是 Load了WebLogic的Antlr!! Hibernate 3.1迅速解决了这个问题