antlr

ANTLR lexing getting confused over '...' and floats

I think the ANTLR lexer is treating my attempt at a range expression "1...3" as a float. The expression "x={1...3}" is coming out of the lexer as "x={.3}" when I used the following token definitions: FLOAT : ('0'..'9')+ ('.' '0'..'9'+)? EXPONENT? | ('.' '0'..'9')+ EXPONENT? ; AUTO : '...'; When I change FLOAT to jus...

Processing an n-ary ANTLR AST one child at a time

I currently have a compiler that uses an AST where all children of a code block are on the same level (ie, block.children == {stm1, stm2, stm3, etc...}). I am trying to do liveness analysis on this tree, which means that I need to take the value returned from the processing of stm1 and then pass it to stm2, then take the value returned ...

Fixed number format in ANTLR

How to specify a fixed digit number in antlr grammar? I want to parse a line which contains fields of fixed number of characters. Each field is a number. 0034|9056|4567|0987|-2340| +345|1000 The above line is a sample line. | indicates field boundaries (which will not be in the actual file. shown here just to indicate the boundary). ...

antlr grammar multiple alternatives

I have this simple grammar for a C# like syntax. I can't figure out any way to separate fields and methods. All the examples I've seen for parsing C# combine fields and methods in the same rule. I would like to split them up as my synatx is pretty simple. grammar test; options { language =CSharp2; k = 3; output = AST; } SE...

How to convert NSString* to ANTLR3_UINT8 or ANTLR3_UINT16

Could somebody tell me what is the proper way to convert a NSString* to an ANTLR3 string (used in C) ? EDIT: this seems to work char buf[255]; if ( YES == [myNSString getCString:buf maxLength:255 encoding:NSStringEncodingConversionAllowLossy] ) { pANTLR3_UINT8 theExpression = (pANTLR3_UINT8*)buf; ... ...

How to write a simple Lexer/Parser with antlr 2.7?

Hello, I have a complex grammar (in antlr 2.7) which I need to extend. Having never used antlr before, I wanted to write a very simple Lexer and Parser first. I found a very good explanation for antlr3 and tried to adapt it: header{ #include <iostream> using namespace std; } options { language="Cpp"; } class P2 extends...

Dynamic typed language example using ANTLR

Hey all, I'm looking for some ANTLR v3 examples, I tried googling a bit but I found certain things which didn't fit my requirments. I found the Mantra project, but it's statically typed and is 'too' big for me at this moment, then I found 'pie' as interpreter, which is dynamically typed, which what I want, but it uses a syntax-directed ...

An interesting project to implement to learn Antlr

Hi, I am learning Antlr from the book 'The Definitive Antlr reference'. I am still in the beginning stages. I like to do hands on stuff, so I was thinking it would be a great learning experience to do a good sample project. I am looking for a decently sized project, not too big. But just big and complicated enough to help me learn Antl...

ANTLR AST rules fail with RewriteEmptyStreamException

I have a simple grammar: grammar sample; options { output = AST; } assignment : IDENT ':=' expr ';' ; expr : factor ('*' factor)* ; factor : primary ('+' primary)* ; primary : NUM | '(' expr ')' ; IDENT : ('a'..'z')+ ; NUM : ('0'..'9')+ ; WS : (' '|'\n'|'\t'|'\r')+ {$channel=HIDDEN;} ; Now ...

problem Keyword token antlr

If the 'for' is used both as a command and as "the English word": for_statement: 'for' ... id: 'for' | ID ; ID: ... right? My problem is how to differentiate the two cases. For example for_statement is only possible beginning of a line (only if preceded by ' ' or '\t'). For example: echo for print example for i in {0..10..2} ...

Coco/R vs. ANTLR

I'm evaluating using Coco/R vs. ANTLR for use in a C# project as part of what's essentially a scriptable mail-merge functionality. To parse the (simple) scripts, I'll need a parser. I've focussed on Coco/R and ANTLR because both seem fairly mature and well-maintained and capable of generating decent C# parsers. Neither seem to be tr...

identifier token keyword antlr parser

How to handle the case where the token 'for' is used in two different situations in the language to parse? Such as statement and as a "parameter" as the following example: echo for print example for i in {0..10..2} do echo "Welcome $i times" done Output: for print example Welcome 0 times Welcome 2 times Welcome 4 times Welcom...

OSLO, ANTLR or other parser grammar, for parsing QUERY EXPRESSION

Greetings I'm working on a project that requires me to write queries in text form, then convert them to some easily processed nodes to be processed by some abiguous repository. Of everything there, the part I'm least interested is the part that converts the text to nodes. I'm hoping it's already done somewhere. Because I'm making stuff...

ANTLR - Embedding Java code, evaluate before or after?

Hello all, I'm writing a simple scripting language on top of Java/JVM, where you can also embed Java code using the {} brackets. The problem is, how do I parse this in the grammar? I have two options: Allow everything to be in it, such as: [a-z|a-Z|0-9|_|$], and go on Get an extra java grammar and use that grammar to parse that small ...

This antlr example is not working properly

Hi, This ANTLR example does not parse input "1;" . Can you explain why? It parses "11;". grammar TestGrammar; options { output=AST; } expr: mexpr (PLUS^ mexpr)* SEMI!; mexpr: atom (STAR^ atom)*; atom: INT; LPAREN: '('; RPAREN: ')'; STAR: '*'; PLUS: '+'; SEMI: ';'; protected DIGIT...

mismatchedtoken with antlr syntactic predicates

I have the following lexer rules in my grammar file: LINE : 'F' | 'G'; RULE : (('->' ('F' | 'G')) => 'F' | 'G' ) | LINE LINE + | LINE * (ROTATE + LINE+)+ ; fragment ROTATE : ('/' | '\\'); I'm basically trying to match productions that look like F -> F/F\F\F/F. It successfully matches stuff like the...

url rewriting with antlr

My java program needs to rewrite urls in html (just in time). I am looking for the right tool and wonder if antlr is doing the job for me? For example: <html><body> <img src="foo.jpg" /> </body></html> should be rewritten as: <html><body> <img src="http://foo.com/foo.jpg" /> </body></html> I want to read/write from/to a stre...

How do I lex this input?

I currently have a working, simple language implemented in Java using ANTLR. What I want to do is embed it in plain text, in a similar fashion to PHP. For example: Lorem ipsum dolor sit amet <% print('consectetur adipiscing elit'); %> Phasellus volutpat dignissim sapien. I anticipate that the resulting token stream would look somethi...

ANTLR: using stringTemplate

(I'm a Noob with Antlr)... I'm having difficulties getting my grammar with StringTemplates. Basically I'm trying to write a little DSL. I can get my grammar the way I want (it parses correctly), but I can't get the generation of the target code to work with templates. So here's a snippet of my grammar: grammar Pfig; options { outp...

Help with parsing a log file (ANTLR3)

I need a little guidance in writing a grammar to parse the log file of the game Aion. I've decided upon using Antlr3 (because it seems to be a tool that can do the job and I figured it's good for me to learn to use it). However, I've run into problems because the log file is not exactly structured. The log file I need to parse looks lik...