antlr

ANTLR 4 and AST visitors

五迷三道 提交于 2019-12-04 10:34:21
问题 I'm trying to use ASTs with ANTLR4, with this files: Builder.java import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public class Builder { public static void main(String[] args) { CharStream input = new ANTLRInputStream("ON M1==2 && M3 == 5 && (M2 > 1 || M5 <= 5.0) " + "DO P5:42 P4:10"); ExprLexer lexer = new ExprLexer(input); TokenStream tokens = new CommonTokenStream

Different lexer rules in different state

假如想象 提交于 2019-12-04 08:42:32
问题 I've been working on a parser for some template language embeded in HTML (FreeMarker), piece of example here: ${abc} <html> <head> <title>Welcome!</title> </head> <body> <h1> Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>! </h1> <p>Our latest product: <a href="${latestProduct}">${latestProduct}</a>! </body> </html> The template language is between some specific tags, e.g. '${' '}', '<#' '>'. Other raw texts in between can be treated like as the same tokens (RAW). The key

Two basic ANTLR questions

喜你入骨 提交于 2019-12-04 08:39:00
I'm trying to use ANTLR to take a simple grammar and produce assembly output. My language of choice in ANTLR is Python. Many tutorials seem very complicated or elaborate on things that aren't relevant to me; I only really need some very simple functionality. So I have two questions: 'Returning' values from one rule to another. So let's say I have a rule like: assignment: name=IDENTIFIER ASSIGNMENT expression; I can run Python code in {}s when this rule is recognised, and I can pass args to the Python code for expression by doing something like: assignment: name=IDENTIFIER ASSIGNMENT expression

Antlr Extraneous Input

拟墨画扇 提交于 2019-12-04 07:34:31
I have a grammar file BoardFile.g4 that has (relevant parts only): grammar Board; //Tokens GADGET : 'squareBumper' | 'circleBumper' | 'triangleBumper' | 'leftFlipper' | 'rightFlipper' | 'absorber' | 'portal' ; NAME : [A-Za-z_][A-Za-z_0-9]* ; INT : [0-9]+ ; FLOAT : '-'?[0-9]+('.'[0-9]+)? ; COMMENT : '#' ~( '\r' | '\n' )*; WHITESPACE : [ \t\r\n]+ -> skip ; KEY : [a-z] | [0-9] | 'shift' | 'ctrl' | 'alt' | 'meta' | 'space' | 'left' | 'right' | 'up' | 'down' | 'minus' | 'equals' | 'backspace' | 'openbracket' | 'closebracket' | 'backslash' | 'semicolon' | 'quote' | 'enter' | 'comma' | 'period' |

How to deal with list return values in ANTLR

て烟熏妆下的殇ゞ 提交于 2019-12-04 07:01:01
What is the correct way to solve this problem in ANTLR: I have a simple grammar rule, say for a list with an arbitrary number of elements. list : '[]' | '[' value (COMMA value)* ']' If I wanted to assign a return value for list, and have that value be the actual list of returned values from the production, what is the proper way to do it? The alternatives I'm entertaining are: create my own stack in the global scope to keep track of these lists Try to inspect the tree nodes below me and extract information that way Access it in some slick and cool way that I'm hoping to find out about in which

ANTLR Ambiguity Issue

ぐ巨炮叔叔 提交于 2019-12-04 06:10:29
问题 I have this grammar. grammar MyGrammar; prog : lexeme* ; lexeme : TOK_INTLIT : [0-9]+; Identifiers : Letter (Letter | Digit | '_' )* ; fragment Letter : [a-zA-Z] ; fragment Digit : [0-9] ; ... Now when I input this: int 2x = 20; I am expecting an error for identifier but I am getting an output like this: Type = TOK_INT value = [int] Line 1, Column 0, Type = TOK_INTLIT value = [2] Line 1, Column 4, Type = Identifiers value = [x] Line 1, Column 5, 2x is being split. Am I doing something wrong

can an element contain attribute as parsed by parser generated by ANTLR? if so, how?

偶尔善良 提交于 2019-12-04 05:52:12
问题 I am following this tutorial and successfully replicated its behavior except that I am using Antlr 4.7 instead of the 4.5 that the tutorial was using. I am trying to build a DSL for expense tracker. Was wondering if each element can have attributes? E.g. this is what it looks like now This is the code for the todo.g4 as seen in https://github.com/simkimsia/learn-antlr-web-js/blob/master/todo.g4 grammar todo; elements : (element|emptyLine)* EOF ; element : '*' ( ' ' | '\t' )* CONTENT NL+ ;

BibTex grammar for ANTLR

时光总嘲笑我的痴心妄想 提交于 2019-12-04 05:51:07
I'm looking for a bibtex grammar in ANTLR to use in a hobby project. I don't want to spend my time for writing ANTLR grammar (this may take some time for me because it will involve a learning curve). So I'd appreciate for any pointers. Note: I've found bibtex grammars for bison and yacc but couldn't find any for antlr. Edit: As Bart pointed the I don't need to parse the preambles and tex in the quoted strings. Here's a (very) rudimentary BibTex grammar that emits an AST (contrary to a simple parse tree): grammar BibTex; options { output=AST; ASTLabelType=CommonTree; } tokens { BIBTEXFILE; TYPE

adding (…) {…} function literals while abstaining from backtracking

亡梦爱人 提交于 2019-12-04 05:19:59
问题 Building off the answer found in How to have both function calls and parenthetical grouping without backtrack, I'd like to add function literals which are in a non LL(*) means implemented like ... tokens { ... FN; ID_LIST; } stmt : expr SEMI // SEMI=';' ; callable : ... | fn ; fn : OPAREN opt_id_list CPAREN compound_stmt -> ^(FN opt_id_list compound_stmt) ; compound_stmt : OBRACE stmt* CBRACE opt_id_list : (ID (COMMA ID)*)? -> ^(ID_LIST ID*) ; What I'd like to do is allow anonymous function

antlr global rule scope declaration vs @members declaration

天涯浪子 提交于 2019-12-04 04:28:04
Which one would you prefer to declare a variable in which case, global scope or @members declaration? It seems to me that they can serve for same purpose? UPDATE here is a grammar to explain what i mean. grammar GlobalVsScope; scope global{ int i; } @lexer::header{package org.inanme.antlr;} @parser::header{package org.inanme.antlr;} @parser::members { int j; } start scope global; @init{ System.out.println($global::i); System.out.println(j); }:R EOF; R:'which one'; Note that besides global (ANTLR) scopes, you can also have local rule-scopes, like this: grammar T; options { backtrack=true; }