grammar

Ambiguous call expression in ANTLR4 grammar

被刻印的时光 ゝ 提交于 2019-11-28 14:32:00
I have a simple grammar (for demonstration) grammar Test; program : expression* EOF ; expression : Identifier | expression '(' expression? ')' | '(' expression ')' ; Identifier : [a-zA-Z_] [a-zA-Z_0-9?]* ; WS : [ \r\t\n]+ -> channel(HIDDEN) ; Obviously the second and third alternatives in the expression rule are ambiguous. I want to resolve this ambiguity by permitting the second alternative only if an expression is immediately followed by a '(' . So the following bar(foo) should match the second alternative while bar (foo) should match the 1st and 3rd alternatives (even if the token between

Examples of LL(1), LR(1), LR(0), LALR(1) grammars?

六眼飞鱼酱① 提交于 2019-11-28 14:24:57
问题 Is there a good resource online with a collection of grammars for some of the major parsing algorithms (LL(1), LR(1), LR(0), LALR(1))? I've found many individual grammars that fall into these families, but I know of no good resource where someone has written up a large set of example grammars. Does anyone know of such a resource? 回答1: Parsing Techniques - A Practical Guide has several examples (i.e. probably half a dozen or so per type) of almost every type of grammar. You can purchase the

Is there a human readable programming language? [closed]

泄露秘密 提交于 2019-11-28 13:55:20
问题 I mean, is there a coded language with human style coding? For example: Create an object called MyVar and initialize it to 10; Take MyVar and call MyMethod() with parameters. . . I know it's not so useful, but it can be interesting to create such a grammar. 回答1: COBOL is a lot like that. SET MYVAR TO 10. EXECUTE MYMETHOD with 10, MYVAR. Another sample from Wikipedia: ADD YEARS TO AGE. MULTIPLY PRICE BY QUANTITY GIVING COST. SUBTRACT DISCOUNT FROM COST GIVING FINAL-COST. Oddly enough though,

Context-free grammars versus context-sensitive grammars?

大憨熊 提交于 2019-11-28 13:32:19
问题 Can someone explain to me why grammars [context-free grammar and context-sensitive grammar] of this kind accepts a String? What I know is Context-free grammar is a formal grammar in which every production(rewrite) rule is a form of V→w Where V is a single nonterminal symbol and w is a string of terminals and/or non-terminals. w can be empty Context-sensitive grammar is a formal grammar in which left-hand sides and right hand sides of any production (rewrite) rules may be surrounded by a

A yacc shift/reduce conflict on an unambiguous grammar

大憨熊 提交于 2019-11-28 13:02:37
A piece of code of my gramamar its driveing me crazy. I have to write a grammar that allow write functions with multiple inputs e.g. function begin a: <statments> b: <statements> end The problem with that its that is statements that are assignments like this ID = Expresion. in the following quote you can see the output produced by yacc. 0 $accept : InstanciasFuncion $end 1 InstanciasFuncion : InstanciasFuncion InstanciaFuncion 2 | InstanciaFuncion 3 InstanciaFuncion : PuntoEntrada Sentencias 4 PuntoEntrada : ID ':' 5 Sentencias : Sentencias Sentencia 6 | Sentencia 7 Sentencia : ID '=' ID State

ANTLR4: Using non-ASCII characters in token rules

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 12:16:49
On page 74 of the ANTRL4 book it says that any Unicode character can be used in a grammar simply by specifying its codepoint in this manner: '\uxxxx' where xxxx is the hexadecimal value for the Unicode codepoint. So I used that technique in a token rule for an ID token: grammar ID; id : ID EOF ; ID : ('a' .. 'z' | 'A' .. 'Z' | '\u0100' .. '\u017E')+ ; WS : [ \t\r\n]+ -> skip ; When I tried to parse this input: Gŭnter ANTLR throws an error, saying that it does not recognize ŭ . (The ŭ character is hex 016D, so it is within the range specified) What am I doing wrong please? ANTLR is ready to

How does the right-shift operator work in a python print statement?

99封情书 提交于 2019-11-28 12:00:53
I've seen someone using "print" with ">>" to write stuffs into a file: In [7]: with open('text', 'w') as f: ...: print >> f, "Hello, world!" ...: In [8]: !type text Hello, world! How does it work? When should I use this instead of just using the "write" method? Ed. From https://docs.python.org/2/reference/simple_stmts.html#the-print-statement print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as “print chevron.” In this form, the first expression after the >> must evaluate to a “file-like” object, specifically an

AST and operator precedence in rule definition

自作多情 提交于 2019-11-28 11:47:24
Hello [¹] I have a simple parser (see below). It intends to parse conditional expressions (relational arithmetic operations and logic combinations thereof). In the example given there, it parses successfully A>5 but then stops and ignores the rest of the input, and this is consistent with my impl. How do I change the expr_ rule to make it parse the entire input? #include <cstdint> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <boost/variant/recursive_wrapper.hpp> namespace qi = boost::spirit::qi;

Are the grammars of modern programming languages context-free or context-sensitive?

假装没事ソ 提交于 2019-11-28 11:11:52
Are the C++, C# or Java languages context-free or context-sensitive? C++ is neither context-free nor context-sensitive, since the template system is Turing-complete and determining whether a piece of C++ code is legal C++ is undecidably hard. For example, I could define a template class that simulates a TM on a string and then creates a constant with value 1 if the machine accepts and 0 if it does not. If I did that, then the following code would be legal iff the TM halted on the given input: int myArray[TMTemplate</* ... args ... */>::value]; Since if the TM rejects, this creates an array of

Xtext: grammar for language with significant/semantic whitespace

老子叫甜甜 提交于 2019-11-28 11:02:45
How can I use Xtext to parse languages with semantic whitespace? I'm trying to write a grammar for CoffeeScript and I can't find any good documentation on this. AFAIK, you can't. In case of parsing Python-like languages, you'd need the lexer to emit INDENT and DEDENT tokens. For that to happen, you'd need semantic predicates to be supported inside lexer rules (Xtext's terminal rules) that would first check if the current-position-in-line of the next character int the input equals 0 (the beginning of the line) and is a ' ' or '\t' . But browsing through the documentation , I don't see this is