grammar

Testing ANTLR Grammar

耗尽温柔 提交于 2019-11-28 10:17:19
So I've been making a grammar in Eclipse with ANTLR v3.4 and I've made one that works and I want to make sure when I edit it everything still works. I can go into the interpretter everytime but that seems like a huge waste of time. Questions: I've read about gunit but the link it gives to download gUnit: ( http://antlr.org/hudson/job/gUnit/org.antlr $gunit/lastSuccessfulBuild/ ) doesn't work. How can I get gUnit. What is the best way to test grammars? Is it actually gUnit or should I just do java tests like jUnit tests? cb4 I recently completed two ANTLR3 assignments (I'm working on my Master

Island grammar antlr3

孤者浪人 提交于 2019-11-28 10:04:28
问题 What are and how to use the "island grammar" in antlr3? 回答1: An island grammar is one that treats most of a language as a blob of text ("water") and picks out the part of the langauge of interest to parse using grammar rules ("island"). For instance, you might choose to build an island grammar to pick out all the expressions found in a C# program, and ignore the variable/method/class declarations and the statement syntax (if, while, ...). The real question is, "Should you use island grammars

Why isn't this a syntax error in python?

醉酒当歌 提交于 2019-11-28 09:34:54
Noticed a line in our codebase today which I thought surely would have failed the build with syntax error, but tests were passing so apparently it was actually valid python (in both 2.x and 3). Whitespace is sometimes not required in the conditional expression: >>> 1if True else 0 1 It doesn't work if the LHS is a variable: >>> x = 1 >>> xif True else 0 File "<stdin>", line 1 xif True else 0 ^ SyntaxError: invalid syntax But it does seem to still work with other types of literals: >>> {'hello'}if False else 'potato' 'potato' What's going on here, is it intentionally part of the grammar for

Dynamically create lexer rule

♀尐吖头ヾ 提交于 2019-11-28 09:33:24
Here is a simple rule: NAME : 'name1' | 'name2' | 'name3'; Is it possible to provide alternatives for such rule dynamically using an array that contains strings? Yes, dynamic tokens match IDENTIFIER rule In that case, simply do a check after the Id has matched completely to see if the text the Id matched is in a predefined collection. If it is in the collection (a Set in my example) change the type of the token. A small demo: grammar T; @lexer::members { private java.util.Set<String> special; public TLexer(ANTLRStringStream input, java.util.Set<String> special) { super(input); this.special =

ANTLR Grammar for expressions

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:59:39
问题 I'm trying to implement a expression handling grammar (that deals with nested parenthesis and stuff). I have the following so far, but they can't deal with some cases (successful/failure cases appear after the following code block). Anyone know what's going on? Note: The varname += and varname = stuff are just some additional AST generation helper stuff in XText. Don't worry about them for now. ... NilExpression returns Expression: 'nil'; FalseExpression returns Expression: 'false';

Can we define a non context-free grammar with ANTLR?

蹲街弑〆低调 提交于 2019-11-28 08:47:16
问题 I'm pretty new to ANTLR4 and now I'm trying to undertand which kind of grammars we might define with it. As far as I got, there're two kind of rules in ANTLR: parser rules (lower case words) and lexer rules (upper-case words). Example: grammar Test; init: prog(','prog)*; prog: A | prog ; A: [a-z]+; Form the grammar production rule standpoint I would say that parser rules are NON-TERMINAL symbols which can be replaced with a sequence of tokens defined by a lexer rules. So, it's perfectly clear

Grammar of a C++ Translation Unit

◇◆丶佛笑我妖孽 提交于 2019-11-28 07:24:00
问题 My understanding, for a long time now, was that a C++ translation unit , after the preprocessor has run, is a sequence of declarations (let me remind that any definition is also a declaration). Many people have argued with this statement but no one has ever given a counterexample. But I myself found this example which troubles me: int x; //declaration ; // ??? EMPTY DECLARATION? int main() //dec { //la } //ration This compiles fine with MSVC and online comeau. I know the standard defines an

How do I generate sentences from a formal grammar?

混江龙づ霸主 提交于 2019-11-28 06:56:01
What's a common way of generating sentences from a grammar? I want an algorithm that's sort of the opposite of a parser. That is, given a formal context-free grammar (say LL), I want to generate an arbitrary sentence that conforms to that grammar. I use sentence here to mean any valid body of text, so it can actually be a whole program (even if it doesn't make any sense—as long as it's syntactially correct). Example grammar: program : <imports> NEWLINE? <namespace> imports : ("import" <identifier> NEWLINE)* namespace : "namespace " <identifier> NEWLINE "{" <classes> "}" identifier: (A-Za-z_)

Lisp grammar in yacc

 ̄綄美尐妖づ 提交于 2019-11-28 06:24:56
I am trying to build a Lisp grammar. Easy, right? Apparently not. I present these inputs and receive errors... ( 1 1) 23 23 23 ui ui This is the grammar... %% sexpr: atom {printf("matched sexpr\n");} | list ; list: '(' members ')' {printf("matched list\n");} | '('')' {printf("matched empty list\n");} ; members: sexpr {printf("members 1\n");} | sexpr members {printf("members 2\n");} ; atom: ID {printf("ID\n");} | NUM {printf("NUM\n");} | STR {printf("STR\n");} ; %% As near as I can tell, I need a single non-terminal defined as a program, upon which the whole parse tree can hang. But I tried it

Antlr4 unexpectedly stops parsing expression

放肆的年华 提交于 2019-11-28 05:43:41
问题 I'm developing a simple calculator with the formula grammar: grammar Formula ; expr : <assoc=right> expr POW expr # pow | MINUS expr # unaryMinus | PLUS expr # unaryPlus | expr PERCENT # percent | expr op=(MULTIPLICATION|DIVISION) expr # multiplyDivide | expr op=(PLUS|MINUS) expr # addSubtract | ABS '(' expr ')' # abs | '|' expr '|' # absParenthesis | MAX '(' expr ( ',' expr )* ')' # max | MIN '(' expr ( ',' expr )* ')' # min | '(' expr ')' # parenthesis | NUMBER # number | '"' COLUMN '"' #