antlr3

Seeking very simple ANTLR error handling example when generating C code

I want to generate C code. I will not be reading from an input file, one line at a time *as, for instance, a compiler might). Rather, I will be parsing user input as it arrives, one line at a time. I would prefer to detect and handle bad input in the lexer/parser, e.g /* lexer tokens */ foo : "FOO"; bar : "BAR"; baz : "BAZ"; /* grammar...

Extending simple ANTLR grammer to support input variables

I'm still on my quest for a really simple language and I know now that there are none. So I'm writing one myself using ANTLR3. I found a really great example in this answer: Exp.g: grammar Exp; eval returns [double value] : exp=additionExp {$value = $exp.value;} ; additionExp returns [double value] : m1=multiplyE...

How do I make a TreeParser in ANTLR3?

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(messa...

How do I match unicode characters in antlr

Hello, I am trying to pick out all tokens in a text and need to match all Ascii and Unicode characters, so here is how I have laid them out. fragment CHAR : ('A'..'Z') | ('a'..'z'); fragment DIGIT : ('0'..'9'); fragment UNICODE : '\u0000'..'\u00FF'; Now if I write my token rule as: TOKEN : (CHAR|DIGIT|UNICODE)+; I get ...

walk an ast in antlr 3

I'm a enthusiast AntlrWorks user. I built a very nice little DSL with it (and antlr of course) that lets me specify interesting data structures using a very simple english-like syntax. The next step is for me to help the user while they type and to do that I would like to have access to the parsing information in exactly the same way as ...

ANTLR3 inject an int into my tree

Is it possible to do what I'm attempting here? Or, perhaps I'm approaching it wrong? arrayDef : { int c = 0; } ('['']' {c++;})+ -> ARRAY /* somehow inject c here */ ; ...

How to generate LLVM IR with ANTLR and C target

Hi, im currently trying to generate LLVM IR with ANTLR3. But the problem ist, that i need the C target (C++ would be better but is not working yet, or is it?) but from C i can't call the LLVM C++ API for Building the IR. The Tutorial from Terence Parr uses Java and the StringTemplate lib. But as i know the StringTemplate lib ist not a...

Is the ANTLR3 C Target Thread Safe?

Hi, Is the ANTLR3 C Target Thread Safe? Thx ...

Using $ as delimiter in StringTemplate from ANTRL rewriter grammars

I'm trying to write an ANTLR3 grammar that generates HTML output using StringTemplate. To avoid having to escape all the HTML tags in the template rules (e.g. \<p\><variable>\</p\>), I'd prefer to use dollar as the delimiter for StringTemplate (e.g. <p>$variable$</p>). While the latter seems to be the default when StringTemplate is used...

ANTLR - basic grammar including unexpected characters?

I've got a really simple ANTLR grammar that I'm trying to get working, but failing miserably at the moment. Would really appreciate some pointers on this... root : (keyword|ignore)*; keyword : KEYWORD; ignore : IGNORE; KEYWORD : ABBRV|WORD; fragment WORD : ALPHA+; fragment ALPHA : 'a'..'z'|'A'..'Z'; fragment ABBRV : WOR...

How can I modify the text of tokens in a CommonTokenStream with ANTLR?

I'm trying to learn ANTLR and at the same time use it for a current project. I've gotten to the point where I can run the lexer on a chunk of code and output it to a CommonTokenStream. This is working fine, and I've verified that the source text is being broken up into the appropriate tokens. Now, I would like to be able to modify the...

ANTLR comment problem

I am trying to write a comment matching rule in ANTLR, which is currently the following: LINE_COMMENT : '--' (options{greedy=false;}: .)* NEWLINE {Skip();} ; NEWLINE : '\r'|'\n'|'\r\n' {Skip();}; This code works fine except in the case that a comment is the last characters of a file, in which case it throws a NoViableAlt exce...

Unusual ANTLR error when attempting to reorganize grammar into two files

I am reorganizing my grammar into two files in order to accomodate a tree grammar; Lua.g and LuaGrammar.g. Lua.g will have all of my lexer rules, and LuaGrammar.g will have all of my tree grammar and parser rules. However, when i try and compile LuaGrammar.g i get the following error: [00:28:37] error(10): internal error: C:\Users\RCIX...

Using C++ types in an ANTLR-generated C parser

I'm trying to use an ANTLR v3.2-generated parser in a C++ project using C as the output language. The generated parser can, in theory, be compiled as C++, but I'm having trouble dealing with C++ types inside parser actions. Here's a C++ header file defining a few types I'd like to use in the parser: /* expr.h */ enum Kind { PLUS, MI...

Using antlr and the DLR together -- AST conversion

I have an AST generated via ANTLR, and I need to convert it to a DLR-compatible one (Expression Trees). However, it would seem that i can't use tree pattern matchers for this as expression trees need their subtrees at instantiation (which i can't get). What solution would be best for me to use? ...

How do I display all pronouns in a sentence and their persons using antlr

EDITED according to WayneH's grammar Here's what i have in my grammar file. grammar pfinder; options { language = Java; } sentence : ((words | pronoun) SPACE)* ((words | pronoun) ('.' | '?')) ; words : WORDS {System.out.println($text);}; pronoun returns [String value] : sfirst {$value = $sfirst.value; System.o...

ANTLR 3.x - How to format rewrite rules

I'm finding myself challenged on how to properly format rewrite rules when certain conditions occur in the original rule. What is the appropriate way to rewrite this: unaryExpression: op=('!' | '-') t=term -> ^(UNARY_EXPR $op $t) Antlr doesn't seem to like me branding anything in parenthesis with a label and "op=" fails. Also, I've...

ANTLR3 runtime metadata

How can I know during parsing which rule is currently matched? I'd like to automatically build an XML (or other objectal hierarchy) representing the parsed input, using rule names, without the need of using grammar actions or trees. Is this possible? Many thanks, Yaakov ...

ANTLR, optional ';' in JavaScript

I'm just playing with ANTLR and decided to try parsing JavaScript with it. But I hit the wall in dealing with optional ';' in it, where statement end is marked by newline instead. Can it be done in some straightforward way? Just a simple grammar example that doesn't work grammar optional_newline; def : statements ; statements ...

problem antlrworks code too large

In Antlrworks I get this error: [18:21:03] Checking Grammar Grammar.g... [18:21:26] Grammar.java:12: code too large [18:21:26] public static final String[] tokenNames = new String[] { [18:21:26] ^ [18:21:26] 1 error Using instead the generated code in a Java project works normally. What can be...