FAQ Database Discussion Community


What is the difference between Lexical grammar and Syntactic grammar?

java,grammar,context-free-grammar,lexical
I am reading The Java Language Specification 8. I am trying to understand Chapter 2. Grammars. Here's what I have already learned: Semantics: Semantics is the study of meaning. Meaning: Meaning, in semantics, is defined as being Extension: The thing in the world that the word/phrase refers to, plus Intention:...

boost::spirit::qi - Working with instance members in grammar struct

c++,grammar,boost-spirit,boost-spirit-qi
I am trying to create a grammar for parsing RTF files. At some points I need to store some information in the scope of the grammar. I know, that I could use inherited attributes, but this would worsen readability. So I asked myself if instance member variables of the grammar...

int* const* foo(int x); is a valid C function prototype. How do you “read” this return type?

c++,c,types,grammar
I notice that this is a valid prototype while reading through the ANSI C grammar spec from 1985 published by Jeff Lee and I compiled a function with this signature. What exactly might a function with this prototype return? What would a simple body of this function look like?

NLTK: Parsing sentences using a simple grammar and part of speech tags

python,parsing,nltk,grammar
For a sentence like "This is a simple sentence" which has been part of speech tagged to: [('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('simple', 'JJ'), ('sentence', 'NN')] And using the following grammar: my_grammar = nltk.CFG.fromstring(""" ... S -> DP VP ... DP -> Det NP ... NP -> Adj N...

signal local to process scope

vhdl,grammar,semantics
I have often wondered why a VHDL variable can be declared both on the process as well as the architecture (as shared) level, while a signal can only be declared on the architecture level - even if it is just used in the scope of a single process. Declaring things...

Given grammar ambiguous or not?

programming-languages,grammar,context-free-grammar,language-theory
Consider the grammar below... bexp -> bterm | bterm ‘||’ bexp bterm -> bfact | bfact ‘&&’ bterm bfact -> true | false | id | ‘(‘ bexp ‘)’ Suppose we extend BEXP with the '!' operator for negation by changing the bfact rule as follows:- bfact -> true |...

Not sure if this LR(1) grammar has shift/reduce conflict

parsing,compiler-construction,programming-languages,grammar,lr
I'm trying to solve question on LR(1) grammar : S->AA A->Aa A-> b I got stuck in state 4 when : S-> AA. ,$ A-> A.a ,$ A->.Aa ,$ A->.a ,$ a and A shifted to other state and A is reduced in the same state should I consider this...

Extending Grammar for LR Parser

parsing,grammar
I have the following grammar for basic arithmetic expressions E -> E + T E -> T T -> T * F T -> F F -> (E) F -> id Where E is expression, T is term, F is factor. I'm wondering how I can extend this grammar to...

Confused at transferring an ambiguous grammar to an unambiguous one

parsing,compiler-construction,grammar,context-free-grammar,ambiguous-grammar
An ambiguous grammar is given and I am asked to rewrite the grammar to make it unambiguous. In fact, I don't know why the given grammar is ambiguous, let alone rewriting it to an unambiguous one. The given grammar is S -> SS | a | b , and I...

How to solve this Grammar through SLR?

parsing,compiler-construction,grammar,left-recursion
I want to solve this Grammar. S->SS+ S->SS* S->a I want to construct SLR sets of items and parsing table with action and goto. Can this grammar parse without eliminate left recursion. Is this Grammar SLR....

Why does Rust put a :: before the parameters in generics sometimes?

rust,grammar,language-design
When declaring a variable of type vector or a hash map in Rust, we do: let v: Vec<int> let m: HashMap<int, int> To instantiate, we need to call new(). However, we do so thusly: Vec::<int>::new() ^^ HashMap::<int, int>::new() ^^ Note the sudden appearance of ::. Coming from C++, these are...

BNF for a hexadecimal number

grammar,context-free-grammar,bnf
As a homework assignment I have to write a BNF definition for a hexadecimal number <hex>. This is to be done using <digit> and <letter>, both of which are defined as follows: <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |...

How to define association on rule without tokens?

parsing,compiler-construction,grammar,yacc,happy
I have the following minimized grammar Exp : let var '=' Exp in Exp end { App (Fn $2 $6) $4 } | Exp Exp { App $1 $2 } | Exp OpCode Exp { Op $1 Add $3 } | '(' Exp ')' { $2 } | num {...

PEGJS: Generating an AST for a predicate first syntax

javascript,parsing,grammar,abstract-syntax-tree,pegjs
I'm back to exploring pegjs and clearly have not grasped the core concept yet. I'm trying to parse a "query language" that starts with a predicate and then a list of operands (which could include another predicate). So a simple example would be: OR( "string1" "string2" ) I would like...

Java DSL implementation using ANTLRv4

java,parsing,antlr,grammar,dsl
So, due to the assignment I was given, I've ended up wondering around what actually the next step might be. To clarify things, I am supposed to implement a DSL language by using Java. The language should enable the "user" the specify and create questionnaire forms based of course on...

How do I do nested parenthesis in antlr4?

nested,grammar,dsl,antlr4,nested-lists
I have the following grammar query : unquoted_str+ | '(' query ')' ; unquoted_str : UNQUOTED_STR ; UNQUOTED_STR : [A-Za-z_][A-Za-z0-9_-]* ; WS : [ \t\r\n]+ -> skip ; I want to be able to execute the following query: abc (def (hij)) But I keep getting the error: line 1:4 extraneous...

Bracket and brace usage in BNF?

syntax,language-agnostic,grammar,bnf,ebnf
Consider the following: A function parameter list is a sequence of zero or more parameters separated by commas and bracketed by parentheses, "(" and ")" . If I want to give the syntax of "function parameter list" assuming that the syntactic category of "parameter" has been defined, can I write:...

SRGS grammars and free-form input in WP 8.1 Speech Recognition

windows-phone-8.1,speech-recognition,grammar
I want my mobile app to have an ability to recognize some commands in order to navigate between pages. I use SRGS grammar to describe commands with the help of xml file. For example, to show News page I use such rules: <rule id="top"> <one-of> <item> <ruleref uri="#newsQuery"/> <tag>out.navigation =...

Troubles with Flex/Bison

grammar,bison,flex-lexer
I try set up grammar using flex/bison by next pattern: DATA: 1,2,3,4,5 PROGRAM: add,mult,div,read This input must be transfered into 4 add mult div read 5 1 2 3 4 5 Where: 4 is a number of commands after "PROGRAM" and 5 is a number of data after "DATA". At...

Grammar: Precedence of grammar alternatives

parsing,antlr,grammar,context-free-grammar
This is a very basic question about grammar alternatives. If you have the following alternative: Myalternative: 'a' | .; Myalternative2: 'a' | 'b'; Would 'a' have higher priority over the '.' and over 'b'? I understand that this may also depend on the behaviour of the parser generated by this...

Unicode version of ABNF?

unicode,grammar,abnf
I want to write a grammar for a file format whose content can contain characters other than US-ASCII ones. Since I am used to ABNF, I try to use it... However, none of RFCs 5234 and 7405 are very friendly towards people who DO NOT use US ASCII. In fact,...

Grammar for parsing PHP like language such that it can handle the PHP begin and end tokens (“”) in the grammar

parsing,grammar,context-free-grammar,lexical-analysis
I am trying to understand how one can define a PHP-like grammar. In PHP, one can get out of PHP mode into HTML mode and then back into PHP mode. For the sake of asking this question, I am defining my PHP-like language to be ridiculously simple. This language will...

ANTLR Grammar for parsing a text file

parsing,antlr,grammar,antlr4
I'm driving crazy trying to generate a parser Grammar with ANTLR. I've got plain text file like: Diagram : VW 503 FSX 09/02/2015 12/02/2015 STP Fleet : AAAA OFF : AAA 05+44 5R06 KKK 05+55 06.04 1R06 5530 ZZZ 06.24 06.30 1R06 5530 YYY 07.53 REVRSE YYY 08.23 9G98 5070...

What constitutes a valid symbol (identifier) in R

r,grammar,identifier
I can’t find a spec of the language… Note that I want a correct answer, e.g. like this, as i could easily come up with a simple, but likely wrong approximation myself, such as [[:alpha:]._][\w._]*...

VoiceXML Grammar Input sequence

grammar,voice-recognition,vxml,voicexml
I need to allow callers to enter their ID. It is 7 characters long and is a mixture of letters and numbers but there is a structure to it. Example: F0G0000 The first character is always a letter. The second character is always a number. The third can be ether...

How can I express this format in EBNF?

python,grammar,ebnf,grako
I have the following data: dbCon= { main = { database = "db1", hostname = "db1.serv.com", maxConnCount = "5", port = "3306", slaves = [ { charset = "utf8", client = "MYSQL", compression = "true", database = "db1_a", hostname = "db1-a.serv.com", maxConnCount = "5", port = "3306", } { charset...

Xtext grammar : mismatched input '0' expecting RULE_INT

grammar,dsl,xtext,ecore
I'm new to Xtext and I'm trying to create a simple DSL for railway systems, here's my grammar : grammar org.xtext.railway.RailWay with org.eclipse.xtext.common.Terminals generate railWay "http://www.xtext.org/railway/RailWay" Model: (trains+=Train)* | (paths+=Path)* | (sections+=Section)* ; Train: 'Train' name=ID ':' 'Path' path=[Path] 'Speed' speed=INT 'end' ; Path: 'Path' name=ID ':' 'Sections' ('{' sections+=[Section]...

How do you parse nested comments in pegjs?

javascript,parsing,grammar,peg,pegjs
I was wondering how do you parse comments (say, a la Haskell), in pegjs. The goal: {- This is a comment and should parse. Comments start with {- and end with -}. If you've noticed, I still included {- and -} in the comment. This means that comments should also...

Why is it not possible to define an XML like element using a context free grammar?

grammar,context-free-grammar,language-theory
Can anyone explain to me why it is not possible to define an XML-like element using a context-free grammar (Chomsky, EBNF or syntax charts)?

ANTLR grammar for SMT formulae

parsing,antlr,grammar,antlr4,smt
I am trying to make a grammar for SMT formulae and this is what I have so far grammar Z3input; startRule : formulaList? EOF; LEFT_PAREN : '('; RIGHT_PAREN : ')'; COMMA : ','; SEMICOLON : ';'; PLUS : '+'; MINUS : '-'; TIMES : '*'; DIVIDE : '/'; DIGIT :...

How can one use a lexer in a package with a parser in a different package?

parsing,antlr,grammar,antlr4
This question concerns Antlr, the parser/lexer generator (Which is pretty awesome IMO). Specifically, the version in question is Antlr4. Currently I'm playing around trying to create a parser/lexer combo in separate files, which worked well at first. However, when I tried to modularize the different components, for organization's sake, I...

Context Free Grammar BNF

grammar,context-free-grammar,bnf,ebnf,ambiguous-grammar
need help with a non-extended BNF grammar: Σ = {a,b,c} L = {ω ɛ Σ^* | such that all a's (if any) comes before all c's(if any)} For example, the strings aba, cbc, and abacbc are in the language, but string abcabc is not. This is what i have so...

How to use symbols and lists in scheme to process data?

scheme,racket,grammar,bnf
I am a newbie in scheme, and I am in the process of writing a function that checks pairwise disjointess of rules (for the time being is incomplete), I used symbols and lists in order to represent the rues of the grammar. Uppercase symbol is a non-terminal in the grammar,...

How to express BNF using Lisp?

lisp,racket,grammar,bnf
I want to express a grammar rule that is written in BNF using Lisp. here is the rule. It is important to note that non-terminals are represented in capital letters and the terminals are represented in small letters: A -> a A b I tried to use the define function...

Creating a regular grammar out of a regular expression

regex,grammar,regular-language
Could anyone please step me through the process of obtaining a regular grammar from a regular expression? I found few 'tutorials', but I am still unable to turn a more complicated regular expression into a grammar. How would you tackle ((a+b)*(c|d))+a?? I though of A -> aB A -> aA...

How to match 0 tokens in JavCC

parsing,grammar,javacc
I'm fairly new at javaCC and I'm trying to create a miniJava parser but I dont know how to skip a semantic action. SKIP : /* Definition of white-space and comments here */ { " " | "\t" | "\n" | "\r" | "\f" | < "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n") >...

javacc java.lang.NullPointerException

parsing,grammar,javacc
im trying to make a miniJava parser but im having trouble figuring out a way to parse method declarations that have no formal parameters. e.g public int getNumber() The code that i have right now works for parameters of one or more, but im not sure how to return an...

Context free grammar for {a*b*c*} - {a^n b^n c^n | n >=0}

grammar,context-free-grammar
I'm having trouble with this problem. I believe its telling me that no string can be generated that has even # of a's b's and c's. This is due to the subtraction of the second set. A good string from a newly formed CFG should be something like aaabbc or...

Can I force ANTL4 to read expected tokens instead of letting it guessing what kind of token it may be?

parsing,grammar,antlr4
I try to write a simple ANTLR4 grammar for parsing SRT subtitles files. I thought it will be an easy, introductory task, but I guess I must miss some point. But first things first --- the grammar: grammar Srt; file : subtitle (NL NL subtitle)* EOF; subtitle: SUBNO NL TSTAMP...

Generate context free grammar for the following language

grammar
**{a^i b^j c^k d^m | i+j=k+m | i<m}** The grammar should allow the language in order abbccd not cbbcda. First should be the a's then b's and so on. I know that you must "count" the number of a's and b's you are adding to make sure there are an...

Is there a way or an algorithm to convert DCG into normal definite clauses in Prolog?

algorithm,parsing,prolog,grammar,dcg
I am a newbie in Prolog, and I am trying to understand how a grammar can be translated into a normal definite clause from a DCG. I understood that DCG notation is just syntactic sugar for normal definite clauses in Prolog. I started to depict some similarities between the normal...

Parse error with simple jison grammar

javascript,parsing,grammar,jison
I am trying to create a simple script language. For the beginning i just wanted stuff like i = 5; i += 3; out(i); So i created the following grammar for jison: %lex %% \s+ { /* ignore */ } "=" { return '='; } "+=" { return '+='; }...

ANTLR is taking the wrong branch

antlr,grammar
I have this very simple grammar: grammar LispExp; expression : LITERAL #LiteralExp | '(' '-' expression ')' #UnaryMinusExp | '(' OP expression expression ')' #OpExp | '(' 'if' expression expression expression ')' #IfExp; OP : '+' | '-' | '*' | '/' | '==' | '<'; LITERAL : '0'|('1'..'9')('0'..'9')*; WS...

bison unexpected identifier error[SOLVED]

grammar,bison,yacc,flex-lexer,lex
I'm getting an "unexpected identifier error", at 1-9, on the line string_op | string_lit { $$ = $1; } here is my grammar %union { int intval; double dubval; char* strval; obj object; } %token <intval> INTEGER %token <dubval> DOUBLE %token <strval> STRING_LITERAL %type <object> number factor value term constant...

C# Get SpeechRecognitionEngine to recognize sentences [duplicate]

c#,speech-recognition,grammar,speech
This question already has an answer here: C# Speech Recognizing Multiple Words together? (Recognize a sentence) 2 answers If I give Microsoft's SpeechRecognitionEngine a Grammar, it will only recognize individual choices in that grammar instead of combinations of choices. Is there any built in way to get it to...

Solving a first-follow conflict in a grammar

grammar,formal-languages,ll
I'm currently having problems solving this kind of a conflict in a grammar: A -> (A)A' A -> 0A' A -> 1A' A'-> NAND A A' A'-> eps The problem is that FIRST of A' is NAND - as well as a part of its FOLLOW set. And since there's...

Context-Free Grammar for Custom Programming Language

parsing,grammar,sml,yacc,context-free-grammar
After having completed the Compiler Design course at my university I have been playing around with making a compiler for a simple programming language, but I'm having trouble with the parser. I'm making the compiler in mosml and using its builtin parser mosmlyac for constructing the parser. Here is an...

Item set and SLR(1) Questions in Compiler

compiler-construction,programming-languages,grammar,ll,lalr
I ran into a Old Exam question that solved by our TA. anyone could help me? when we create SLR(1) about S--> aSb | a grammar, one of the item set LR(0) is like as: { S-->a.Sb, S-->a., S-->.aSb, S-->.a} about extracted rule from above set, which of them is...

Grammar to parse sql statements delimited by semicolon in antlr4

grammar,antlr4
I'm looking to separate sql statements that could have comments /* */ or strings 'test' or line comments --line comment (sql style) separated by semicolons. An example would be: Blah blah 'string ; ' ; More text /* semicolon(;) inside comment */ Some more text in multiple lines the text...

Generate context free grammar for the following language:

grammar
The set of string from the alphabet {j,k} where the string can be reversed and then all j's changed to k's and all k's changed to j's. For example "jjkk" would be in the language because when you reverse it: "kkjj" and when you flip all of the characters to...

Why do we need semicolon before closing brace? [closed]

c++,grammar
In C++, why do we need semicolon before closing brace? It's strange that something like { statement; statement; statement } is not possible, that semicolon not only delimiter statements. I understand that semicolon after class definition is necessary because there may be a definition of objects of this class. But...