Search Results

Search found 174 results on 7 pages for 'antlr'.

Page 2/7 | < Previous Page | 1 2 3 4 5 6 7  | Next Page >

  • ANTLR - accessing token values in c/c++

    - by Bernhard Schenkenfelder
    Hello, I am trying to parse integers and to access their value in antlr 3.2. I already found out how to do this in Java: //token definition INT : '0'..'9'+; //rule to access token value: start : val=INT {Integer x = Integer.valueOf( $val.text ).intValue(); } ; ... but I couldn't find a solution for this in C/C++. Does someone know how to do this? Bernie

    Read the article

  • Fixed number format in ANTLR

    - by sarav
    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. The fields can include blank characters +/-

    Read the article

  • This antlr example is not working properly

    - by Aftershock
    Hi, This ANTLR example does not parse input "1;" . Can you explain why? It parses "11;". grammar test; options {//language = 'CSharp2'; //language = 'Java'; output=AST; } expr : mexpr (PLUS^ mexpr)* SEMI! ; mexpr : atom (STAR^ atom)* ; atom: INT ; //class csharpTestLexer extends Lexer; WS : (' ' | '\t' | '\n' | '\r') { $channel = HIDDEN; } ; LPAREN: '(' ; RPAREN: ')' ; STAR: '*' ; PLUS: '+' ; SEMI: ';' ; protected DIGIT : '0'..'9' ; INT : (DIGIT)+ ;

    Read the article

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

    - by Jay Allard
    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 up as I go, I chose to use a LINQish expression syntax. from m in Movie select m.A, m.B I started parsing it manually and got the basics, but it's pretty cheesy. I'm looking for the better solution. I made some progress using MGrammar, but it would be nice if such a thing already existed. Does anyone know of anything that already does this? I looked for existing ANTLR templates, but no luck. Thanks for the help.

    Read the article

  • Visualizing an AST created with ANTLR (in a .Net environment)

    - by Benjamin Podszun
    Hi there. For a pet project I started to fiddle with ANTLR. After following some tutorials I'm now trying to create the grammar for my very own language and to generate an AST. For now I'm messing around in ANTLRWorks mostly, but now that I have validated that the parse tree seems to be fine I'd like to (iteratively, because I'm still learning and still need to make some decisions regarding the final structure of the tree) create the AST. It seems that antlrworks won't visualize it (or at least not using the "Interpreter" feature, Debug's not working on any of my machines). Bottom line: Is the only way to visualize the AST the manual way, traversing/showing it or printing the tree in string representation to a console? What I'm looking for is a simple way to go from input, grammar - visual AST representation a la the "Interpreter" feature of ANTLRWorks. Any ideas?

    Read the article

  • Dynamic typed language example using ANTLR

    - by wvd
    Hey all, I'm looking for some ANTLR 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 interpreter. I'm looking for a pretty small language which is dynamically typed and uses AST's if possible. It doesn't need to be advanced, if it would have classes I would already be very happy. Thanks, William van Doorn

    Read the article

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

    - by Andy Hull
    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 just check for integers, as so: FLOAT : ('0'..'9')+; then the expression "x={1...3}" is tokenized correctly. Can anyone help me to fix this? Thanks!

    Read the article

  • how to check an ANTLR token is only used once or less in the parser

    - by Simon Kenyon Shepard
    In Antlr, if I have a rule for example: someRule : TOKENA TOKENB; it would accept : "tokena tokenb" if I would like TOKENA to be optional, I can say, someRule : TOKENA* TOKENB; then I can have : "tokena tokenb" or "tokenb" or "tokena tokena tokenb" but this also means it can be repeated more that once. Is there anyway I can say this token can be there 1 or less times but not more than one? so it would accept: "tokena tokenb" or "tokenb" BUT NOT "tokena tokena tokenb"? Many thanks

    Read the article

  • Java3d shape with Antlr

    - by Eldeus
    Well how to evaluate a very simple antlr grammar that does only this. Box(1,2,4) Cylinder(1,2) and builds java3d shapes, (given I have already built a canvas for java3d and have the code for creating each element in java, protected static BranchGroup addBox1(Float a, Float b, Float C){ // create branch for display TransformGroup bodyTransform = new TransformGroup(); BranchGroup bg = new BranchGroup(); bg.setCapability(BranchGroup.ALLOW_DETACH); bg.setUserData(shapeId); // set transformation bodyTransform = setTransformShape(0,0,0,0,0,0,0); // create box Box tmpBox = new Box(a,b,c, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS,setAppearance(color)); getCoords(tmpBox); bodyTransform.addChild(tmpBox); trFormList.add(bodyTransform); shapeId++; //add box to branch bg.addChild(bodyTransform); return bg; } ) thanks

    Read the article

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

    - by Chris Lieb
    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 by stm2 and pass it to stm3, and so on. I do not see a way of executing the child rules in this fashion when the AST is structured this way. Is there a way to allow me to chain the execution of the child grammar items with my given AST, or am I going to have to go through the painful process of refactoring the parser to generate a nested structure and updating the rest of the compiler to work with the new AST? Example ANTLR grammar fragment: block : ^(BLOCK statement*) ; statement : // stuff ; What I hope I don't have to go to: block : ^(BLOCK statementList) ; statementList : ^(StmLst statement statement+) | ^(StmLst statement) ; statement : // stuff ;

    Read the article

  • Replace token in ANTLR

    - by user1019710
    I want to replace a token using ANTLR. I tried with TokenRewriteStream and replace, but it didn't work. Any suggestions? ANTLRStringStream in = new ANTLRStringStream(source); MyLexer lexer = new MyLexer(in); TokenRewriteStream tokens = new TokenRewriteStream(lexer); for(Object obj : tokens.getTokens()) { CommonToken token = (CommonToken)obj; tokens.replace(token, "replacement"); } The lexer finds all occurences of single-line comments, and i want to replace them in the original source too.

    Read the article

  • Getting tree construction with ANTLR

    - by prosseek
    As asked and answered in http://stackoverflow.com/questions/2999755/removing-left-recursion-in-antlr , I could remove the left recursion E - E + T|T T - T * F|F F - INT | ( E ) After left recursion removal, I get the following one E - TE' E' - null | + TE' T - FT' T' - null | * FT' Then, how to make the tree construction with the modified grammar? With the input 1+2, I want to have a tree ^('+' ^(INT 1) ^(INT 2)). Or similar. grammar T; options { output=AST; language=Python; ASTLabelType=CommonTree; } start : e - e ; e : t ep - ??? ; ep : | '+' t ep - ??? ; t : f tp - ??? ; tp : | '*' f tp - ??? ; f : INT | '(' e ')' - e ; INT : '0'..'9'+ ; WS: (' '|'\n'|'\r')+ {$channel=HIDDEN;} ;

    Read the article

  • antlr line after line processing

    - by pawloch
    I'm writing simple language in ANTLR, and I'd like to write shell where I can put line of code, hit ENTER and have it executed, enter another line, and have it executed. I have already written grammar which execute all alines of input at one. Example input: int a,b,c; string d; string e; d=\"dziala\"; a=4+7; b=a+33; c=(b/11)*2; grammar Kalkulator; options { language = Java; output=AST; ASTLabelType=CommonTree; } tokens { NEG; } @header { package lab4; } @lexer::header { package lab4; } line : (assignment | declaration)* EOF ; declaration : type^ IDENT (','! IDENT)* ';'! ; type : 'int' | 'string' ; assignment : IDENT '='^ expression ';'! ; term : IDENT | INTEGER | STRING_LITERAL | '('! expression ')'! ; unary : (( negation^ | '+'! ))* term ; negation : '-' -> NEG ; mult : unary ( ('*'^ | '/'^) unary )* ; exp2 :mult ( ('-'^ | '+'^) mult)* ; expression : exp2 ('&'^ exp2)* ; fragment LETTER : ('a'..'z'|'A'..'Z'); fragment DIGIT : '0'..'9'; INTEGER : DIGIT+; IDENT : LETTER (LETTER | DIGIT)* ; WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel=HIDDEN;}; STRING_LITERAL : '\"' .* '\"'; and: tree grammar Evaluator; options { language = Java; tokenVocab = Kalkulator; ASTLabelType = CommonTree; } @header { package lab4; import java.util.Map; import java.util.HashMap; } @members { private Map<String, Object> zmienne = new HashMap<String, Object>(); } line returns [Object result] : (declaration | assignment { result = $assignment.result; })* EOF ; declaration : ^(type ( IDENT { if("string".equals($type.result)){ zmienne.put($IDENT.text,""); //add definition } else{ zmienne.put($IDENT.text,0); //add definition } System.out.println($type.result + " " + $IDENT.text);//write output } )* ) ; assignment returns [Object result] : ^('=' IDENT e=expression) { if(zmienne.containsKey($IDENT.text)) {zmienne.put($IDENT.text, e); result = e; System.out.println(e); } else{ System.out.println("Blad: Niezadeklarowana zmienna"); } } ; type returns [Object result] : 'int' {result="int";}| 'string' {result="string";} ; expression returns [Object result] : ^('+' op1=expression op2=expression) { result = (Integer)op1 + (Integer)op2; } | ^('-' op1=expression op2=expression) { result = (Integer)op1 - (Integer)op2; } | ^('*' op1=expression op2=expression) { result = (Integer)op1 * (Integer)op2; } | ^('/' op1=expression op2=expression) { result = (Integer)op1 / (Integer)op2; } | ^('%' op1=expression op2=expression) { result = (Integer)op1 \% (Integer)op2; } | ^('&' op1=expression op2=expression) { result = (String)op1 + (String)op2; } | ^(NEG e=expression) { result = -(Integer)e; } | IDENT { result = zmienne.get($IDENT.text); } | INTEGER { result = Integer.parseInt($INTEGER.text); } | STRING_LITERAL { String t=$STRING_LITERAL.text; result = t.substring(1,t.length()-1); } ; Can I make it process line-by-line or is that easier to make it all again?

    Read the article

  • ANTLR AST rules fail with RewriteEmptyStreamException

    - by Barry Brown
    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 I want to add some rewrite rules to generate an AST. From what I've read online and in the Language Patterns book, I should be able to modify the grammar like this: assignment : IDENT ':=' expr ';' -> ^(':=' IDENT expr) ; expr : factor ('*' factor)* -> ^('*' factor+) ; factor : primary ('+' primary)* -> ^('+' primary+) ; primary : NUM | '(' expr ')' -> ^(expr) ; But it does not work. Although it compiles fine, when I run the parser I get a RewriteEmptyStreamException error. Here's where things get weird. If I define the pseudo tokens ADD and MULT and use them instead of the tree node literals, it works without error. tokens { ADD; MULT; } expr : factor ('*' factor)* -> ^(MULT factor+) ; factor : primary ('+' primary)* -> ^(ADD primary+) ; Alternatively, if I use the node suffix notation, it also appears to work fine: expr : factor ('*'^ factor)* ; factor : primary ('+'^ primary)* ; Is this discrepancy in behavior a bug?

    Read the article

  • antlr grammar multiple alternatives

    - by joe
    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; } SEMI : ';' ; LCURLY : '{' ; RCURLY : '}' ; LPAREN : '(' ; RPAREN : ')' ; DOT :'.'; IDENTIFIER : ( 'a'..'z' | 'A'..'Z' | '_' ) ( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' )* ; namespaceName : IDENTIFIER (DOT IDENTIFIER)* ; classDecl : 'class' IDENTIFIER LCURLY (fieldDecl | methodDecl)* RCURLY ; fieldDecl : namespaceName IDENTIFIER SEMI; methodDecl : namespaceName IDENTIFIER LPAREN RPAREN SEMI; I always end up wit this warning Decision can match input such as "IDENTIFIER DOT IDENTIFIER" using multiple alternatives: 1, 2

    Read the article

  • Antlr Lexer Quoted String Problem

    - by Loki
    I'm trying to build a lexer to tokenize lone words and quoted strings. I got the following: STRING: QUOTE (options {greedy=false;} : . )* QUOTE ; WS : SPACE+ { $channel = HIDDEN; } ; WORD : ~(QUOTE|SPACE)+ ; For the corner cases, it needs to parse: "string" word1" word2 As three tokens: "string" as STRING and word1" and word2 as WORD. Basically, if there is a last quote, it needs to be part of the WORD were it is. If the quote is surrounded by white spaces, it should be a WORD. I tried this rule for WORD, without success: WORD: ~(QUOTE|SPACE)+ | (~(QUOTE|SPACE)* QUOTE ~QUOTE*)=> ~(QUOTE|SPACE)* QUOTE ~(QUOTE|SPACE)* ;

    Read the article

  • grammar parser lexer antlr letteral

    - by BB
    What's the difference between this grammar: ... if_statement : 'if' condition 'then' statement 'else' statement 'end_if'; ... and this: ... if_statement : IF condition THEN statement ELSE statement END_IF; ... IF : 'if'; THEN: 'then'; ELSE: 'else'; END_IF: 'end_if'; .... ? If there is any difference, as this impacts on performance ... Thanks

    Read the article

  • Antlr Lexer Quoted String Predicate

    - by Loki
    I'm trying to build a lexer to tokenize lone words and quoted strings. I got the following: STRING: QUOTE (options {greedy=false;} : . )* QUOTE ; WS : SPACE+ { $channel = HIDDEN; } ; WORD : ~(QUOTE|SPACE)+ ; For the corner cases, it needs to parse: "string" word1" word2 As three tokens: "string" as STRING and word1" and word2 as WORD. Basically, if there is a last quote, it needs to be part of the WORD were it is. If the quote is surrounded by white spaces, it should be a WORD. I tried this rule for WORD, without success: WORD: ~(QUOTE|SPACE)+ | (~(QUOTE|SPACE)* QUOTE ~QUOTE*)=> ~(QUOTE|SPACE)* QUOTE ~(QUOTE|SPACE)* ;

    Read the article

  • Writing language converter in ANTLR

    - by Stefan
    I'm writing a converter between some dialects of the same programming language. I've found a grammar on the net - it's complex and handles all the cases. Now I'm trying to write the appropriate actions. Most of the input is just going to be rewritten to output. What I need to do is parse function calls, do my magic (rename function, reorder arguments, etc) and write it. I'm using AST as output. When I come across a function call, I build a custom object structure (from classes defined in my target language), call the appropriate function and I have a string that represents the transformed function that I want to get. The problem is, what I'm supposed to do with that string? I'd like to replace the .text attribute of the enclosing rule, but setText() is only available on lexer rules and the rule's .text attribute is read-only. How to solve this problem? program : statement_list { output = $statement_list.text; } ; //... statement : expression_statement // ... ; expression_statement : function_call // ... ; function_call : ID '(' { /* build the object, assign name */ Function function = new Function(); //... } ( arg1 = expression { /* add first parameter */ } ( ',' arg2 = expression { /* add the rest of parameters */ } )* )? ')' { /* convert the function call */ string converted = Tools.Convert(function); // $setText(converted); // doesn't work // $functionCall.text = converted; // doesn't work } ;

    Read the article

  • problem Keyword token antlr

    - by batman_for
    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'). Thanks.

    Read the article

  • identifier token keyword antlr parser

    - by batman_for
    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 Welcome 6 times Welcome 8 times Welcome 10 times Thanks.

    Read the article

  • ANTLR - Embedding Java code, evaluate before or after?

    - by wvd
    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: 1] Allow everything to be in it, such as: [a-z|a-Z|0-9|_|$], and go on 2] Get an extra java grammar and use that grammar to parse that small code (is it actually possible and efficient?) Since option 2] is basically a double-check since when evaluating java code it's also being checked. Now my last question is -- is way that can dynamically execute java code also with objects which have been created at runtime? Thanks, William van Doorn

    Read the article

< Previous Page | 1 2 3 4 5 6 7  | Next Page >