context-free-grammar

Left recursion elimination

一世执手 提交于 2019-12-02 08:35:30
I have this grammar S->S+S|SS|(S)|S*|a I want to know how to eliminate the left recursion from this grammar because the S+S is really confusing... Let's see if we can simplify the given grammar. S -> S*|S+S|SS|(S)|a We can write it as; S -> S*|SQ|SS|B|a Q -> +S B -> (S) Now, you can eliminate left recursion in familiar territory. S -> BS'|aS' S' -> *S'|QS'|SS'|e Q -> +S B -> (S) Note that e is epsilon/lambda. We have removed the left recursion, so we no longer have need of Q and B. S -> (S)S'|aS' S' -> *S'|+SS'|SS'|e You'll find this useful when dealing with left recursion elimination . My

Context-Free Grammar suggestions for unary subtraction?

孤者浪人 提交于 2019-12-02 05:39:13
L = {1 i - 1 j = 1 i-j : i-j >= 0, i,j>=0} I'm confused about how to construct a grammar that tracks subtraction of a string element. I have no clue on how to get started on this and attempted to work with an equivaent construction of the form L = {1 i = 1 i-j + 1 j } Any hints or suggrstions are appreciated. Here's a tip: always try to think of context-free languages in terms of parenthetic balancing. Consider the following two languages: ab () aabb (()) aaabbb ((())) aaaabbbb (((()))) ... ... Most of us "see" the first language as a repetition (some a's followed by the same number of b's)

Closure properties of context free languages

混江龙づ霸主 提交于 2019-12-02 04:50:22
I have the following problem: Languages L1 = {a^n * b^n : n>=0} and L2 = {b^n * a^n : n>=0} are context free languages so they are closed under the L1L2 so L={a^n * b^2n A^n : n>=0} must be context free too because it is generated by a closure property. I have to prove if this is true or not. So I checked the L language and I don’t think that it is context free then I also saw that L2 is just L1 reversed. Do I have to check if L1, L2 are deterministic? L1={a n b n : n>=0} and L2={b n a n : n>=0} are both context free. Since context-free languages are closed under concatenation, L3=L1L2 is also

Is C# considered a context free language?

人盡茶涼 提交于 2019-12-01 20:57:22
问题 I have been looking for this, but there is a lot of different answers to this question on the MSDN forums. Some people say that "All computer language grammars are context free" and others say that any language that has white-space sensitive syntax is likely context-sensitive and therefore not context-free (F# and Python). Whould be nice a definitive answer and maybe some proof. 回答1: I would describe C# as having a context-free grammar, but the language has context-sensitive rules that are

Is C# considered a context free language?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 18:41:23
I have been looking for this, but there is a lot of different answers to this question on the MSDN forums . Some people say that "All computer language grammars are context free" and others say that any language that has white-space sensitive syntax is likely context-sensitive and therefore not context-free (F# and Python). Whould be nice a definitive answer and maybe some proof. Mike Zboray I would describe C# as having a context-free grammar, but the language has context-sensitive rules that are not expressed in the grammar. From Wikipedia ( Formal grammar ): A context-free grammar is a

Combine free-form dictation and semantic in a srgs grammar

只谈情不闲聊 提交于 2019-12-01 14:11:54
I'm trying to combine both the result of a semantic and a dictation request in the semantic value of a SRGS document. For example, I would say "Search potato" and the output would be something like out="Search Potato" where Potato is a random word spoken by the user. I tought about using the garbage special rule, but it doesn't seem to work. So far that's what I have : <rule id="rule1" scope="public"> <one-of> <item xml:lang="en-us">Search</item> <item>Cherche</item> </one-of> <tag>out.command="Search"</tag> <tag>out.param1=<ruleref special="GARBAGE"/></tag> <tag>out=out.command+out.param1;<

Operator associativity using Scala Parsers

独自空忆成欢 提交于 2019-12-01 13:02:19
So I've been trying to write a calculator with Scala's parser, and it's been fun, except that I found that operator associativity is backwards, and that when I try to make my grammar left-recursive, even though it's completely unambiguous, I get a stack overflow. To clarify, if I have a rule like: def subtract: Parser[Int] = num ~ "-" ~ add { x => x._1._1 - x._2 } then evaluating 7 - 4 - 3 comes out to be 6 instead of 0. The way I have actually implemented this is that I am composing a binary tree where operators are non-leaf nodes, and leaf nodes are numbers. The way I evaluate the tree is

Using C++11 regex to capture the contents of a context-free-grammar file

痴心易碎 提交于 2019-12-01 07:34:05
问题 Preface I'm trying to write my own context-free-grammar specification, to associate with the rules of my lexer/parser. It is meant to be similar to that of ANTLR's, where upper-case identifiers classify as a Lexer rule and lower-case identifiers classify as a Parser rule. It is meant to accept any combination of string literals and/or regular expressions for lexer rules, and any combination of lexer/regex rules and/or other parser identifiers for parser rules. Each rule in is the format of

Are there tools to convert between ANTLR and other forms of BNF?

久未见 提交于 2019-12-01 03:23:41
Are there any tools to convert ANTLR grammar syntax to and from other BNF syntaxes? There are several forms Backus-Naur Form (BNF, EBNF, ABNF, W3C-BNF, XBNF...) with specification, e.g. see this list . The ANTLR grammar syntax only seems to be described by examples . I know that ANTLR grammar files contain more than the specification of a context-free syntax, but you should be able to convert at least the common subset - has anyone done yet automatically? Jakob wrote: The ANTLR grammar syntax only seems to be described by examples. ANTLR (v3) is written "in its own words" (as Terence Parr

Using Ogden’s Lemma versus regular Pumping Lemma for Context-Free Grammars

落花浮王杯 提交于 2019-12-01 01:35:34
问题 I'm learning the difference between the lemmata in the question. Every reference I can find uses the example: {(a^i)(b^j)(c^k)(d^l) : i = 0 or j = k = l} to show the difference between the two. I can find an example using the regular lemma to "disprove" it. Select w = uvxyz, s.t. |vy| > 0, |vxy| <= p. Suppose w contains an equal number of b's, c's, d's. I selected: u,v,x = ε y = (the string of a's) z = (the rest of the string w) Pumping y will just add to the number of a's, and if |b|=|c|=|d|