context-free-grammar

LR(1) Item DFA - Computing Lookaheads

喜欢而已 提交于 2020-01-11 16:40:09
问题 I have trouble understanding how to compute the lookaheads for the LR(1)-items. Lets say that I have this grammar: S -> AB A -> aAb | a B -> d A LR(1)-item is an LR(0) item with a lookahead. So we will get the following LR(0)-item for state 0: S -> .AB , {lookahead} A -> .aAb, {lookahead} A -> .a, {lookahead} State: 1 A -> a.Ab, {lookahead} A -> a. ,{lookahead} A -> .aAb ,{lookahead} A ->.a ,{lookahead} Can somebody explain how to compute the lookaheads ? What is the general approach ? Thank

Operator associativity using Scala Parsers

蓝咒 提交于 2020-01-11 07:11:07
问题 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

Pumping Lemma with Context Free Languages

杀马特。学长 韩版系。学妹 提交于 2020-01-10 19:42:09
问题 I have the language {a^i b^j c^k | i,j,k>=0 & i>j & j>k} I began by assuming some m is picked for me, such that a string z = a^m b^(m-1) c^(m-2) Then the string is split up into (z =) uvwxy so that vx are not empty and #(vwx)<=m Then when I get to pick an " i " I get confused. Say I pick i=1 then I have: uv^1wx^1y and I'm not entirely sure where to go from that because to me it looks like I could pick a vwx that IS in the language. Any suggestions? 回答1: I'd begin by picking a slightly better

right linear context free grammar

好久不见. 提交于 2020-01-07 06:38:28
问题 I've a problem. I have to write right linear context free grammar with alphapet={0,1} where the numbers of 0 will be even and the numbers od 1 will be odd. I tried write sth. but it's doesn't work. s --> [1],a. s --> [0],b. a --> []. a --> [1],c. a --> [0],b. c --> [1],k. c --> [0],b. b --> [0],k. b --> [1],d. d --> [1],b. d --> [0],c. k --> []. k --> s. Grammar should accept even amount of 0s and odd amount of 1s. Grammar context free is right linear when A->wB or A->w where w is any word

Seemingly equivalent Menhir rules change the shift/reduce conflicts found in grammar

你离开我真会死。 提交于 2020-01-06 03:05:40
问题 I'm using Menhir to create a parser, and there's a behaviour that always trips me, and I don't understand it. I have created the following minimal example to demonstrate it; this shows the declaration of the receiver argument in a method declaration in the Go language (http://golang.org/ref/spec#Method_declarations): %{ %} %token <string> T_identifier %token T_star %start <unit> demo %% (* This rule has a shift/reduce conflict demo: | option(T_identifier) option(T_star) T_identifier { () } *)

Concatenation shift-reduce conflict

别等时光非礼了梦想. 提交于 2020-01-04 06:22:09
问题 I have a simple grammar for JavaCUP's LR(1) parser that recognises concatenation expressions of identifiers and strings. I also want to add some empty function calls as a possible concatenation argument. However, when I try that, it leads to a shift/reduce conflict. Grammar: precedence left PLUS; e ::= e exp | exp; exp ::= concat | literal; concatenation ::= exp PLUS exp | LPAREN exp RPAREN; literal ::= IDENTIFIER | STRING | IDENTIFIER LPAREN RPAREN; // THIS PRODUCES THE ERROR Input: x + x +

Real-world LR(k > 1) grammars?

[亡魂溺海] 提交于 2020-01-01 18:05:10
问题 Making artificial LR(k) grammars for k > 1 is easy: Input: A1 B x Input: A2 B y (introduce reduce-reduce conflict for terminal a) A1 : a A2 : a B : b b b ... b (terminal b occurs k-1 times) However, are there any real-world non-LR(1) computer languages that are LR(k > 1)-parsable? Or are non-LR(1) languages also not LR(k) either? 回答1: If a language has an LR(k) grammar, then it has an LR(1) grammar which can be generated mechanically from the LR(k) grammar; furthermore, the original parse

How does the CYK algorithm work?

白昼怎懂夜的黑 提交于 2020-01-01 12:11:09
问题 I have to check if a string can be derived from a given context free that is in Chomsky normal form. I'm using C++. There is very nice pseudocode on the Wikipedia article covering the CYK algorithm, but I can't understand it very well. Would someone be so kind to help me out by giving me another pseudocode for CYK algorithm, or maybe explain the one in the wiki article? 回答1: The CYK algorithm takes as input a CFG that's in Chomsky normal form. That means that every production either has the

How to construct a CFG based on a given regular expression

大兔子大兔子 提交于 2020-01-01 12:06:13
问题 I am trying to figure out how to construct a CFG (context free grammar) based on a given regular expression. For example, a(ab)*(a|b) I think there is an algorithm to go through, but it is really confusing. here is what i got so far: S->aAB; A->aAb|empty; B->a|b; Does this look right? Any help would be appreciated. 回答1: Construct the CFG in three parts, each for a , (ab)* and (a|b) . For (a|b) , you've got B -> a | b right. (ab)* would mean strings like ab , abab , ababab and so on. So A ->

Horizontal Markovization

一笑奈何 提交于 2020-01-01 05:40:18
问题 I have to implement horizontal markovization (NLP concept) and I'm having a little trouble understanding what the trees will look like. I've been reading the Klein and Manning paper, but they don't explain what the trees with horizontal markovization of order 2 or order 3 will look like. Could someone shed some light on the algorithm and what the trees are SUPPOSED to look like? I'm relatively new to NLP. 回答1: So, let's say you have a bunch of flat rules like: NP NNP NNP NNP NNP or VP V Det