context-free-grammar

Shift-reduce: when to stop reducing?

做~自己de王妃 提交于 2019-12-05 11:41:16
I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using recursive rules that enforce order of operations, inspired by the ANSI C Yacc grammar : S: A; P : NUMBER | '(' S ')' ; M : P | M '*' P | M '/' P ; A : M | A '+' M | A '-' M ; And we want to parse 1+2 using shift-reduce parsing. First, the 1 is shifted as a NUMBER. My question is, is it then reduced to P, then M, then A, then finally S? How does it know where to stop? Suppose it does reduce all the way to S, then shifts '+'. We'd now have a stack containing: S '+' If we shift '2', the reductions might

Making a Grammar LL(1)

自闭症网瘾萝莉.ら 提交于 2019-12-05 04:46:41
I have the following grammar: S → a S b S | b S a S | ε Since I'm trying to write a small compiler for it, I'd like to make it LL(1). I see that there seems to be a FIRST/FOLLOW conflict here, and I know I have to use substitution to resolve it, but I'm not exactly sure how to go about it. Here is my proposed grammar, but I'm not sure if it's correct: S-> aSbT | epsilon T-> bFaF| epsilon F-> epsilon Can someone help out? templatetypedef In his original paper on LR parsing , Knuth gives the following grammar for this language, which he conjectures "is the briefest possible unambiguous grammar

Context free grammar for non-palindrome

好久不见. 提交于 2019-12-04 21:59:31
问题 I need a CFG which will generate strings other than palindromes. The solution has been provided and is as below.(Introduction to theory of computation - Sipser) R -> XRX | S S -> aTb | bTa T -> XTX | X | <epsilon> X -> a | b I get the general idea of how this grammar works. It mandates the insertion of a sub-string which has corresponding non-equal alphabets on its either half, through the production S -> aTb | bTa , thus ensuring that a palindrome could never be generated. I will write down

Writing grammar rules for context sensitive elements using Pyparsing

给你一囗甜甜゛ 提交于 2019-12-04 16:10:31
I am trying to write a grammar for a set of sentences and using Pyparsing to parse it. These sentences tell what and how to search in a text file, and I need to convert them into corresponding regex search codes. However, there are some elements that are not really context-free and hence, I am finding it difficult to write production rules for them. Basically, my aim is to parse these sentences and then write regexes for them. Some examples of context-sensitive elements found in these sentences - LINE_CONTAINS phrase1 BEFORE {phrase2 AND phrase3} means in the line, phrase1 can come anywhere

Algorithm to generate context free grammar from any regex

核能气质少年 提交于 2019-12-04 12:34:07
问题 Can anyone outline for me an algorithm that can convert any given regex into an equivalent set of CFG rules? I know how to tackle the elementary stuff such as (a|b)*: S -> a A S -> a B S -> b A S -> b B A -> a A A -> a B A -> epsilon B -> b A B -> b B B -> epsilon S -> epsilon (end of string) However, I'm having some problem formalizing it into a proper algorithm especially with more complex expressions that can have many nested operations. 回答1: If you are just talking about regular

How to construct a CFG based on a given regular expression

人盡茶涼 提交于 2019-12-04 12:15:59
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. 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 -> abA | empty would be the correct production. Hence, the full grammar becomes: S -> aAB A -> abA | empty B ->

How to enumerate the strings of a context-free grammar?

瘦欲@ 提交于 2019-12-04 07:19:42
What algorithm do you use to enumerate the strings generated by a context-free grammar? It seems doable when there is no recursion, but I can't figure out how to do it in the general case, which might contain all kinds of (possibly indirect) recursion. (I'm not looking for an esoteric solution like the one on this page ; I'm looking for an algorithm that I could map to standard imperative code.) Here's an obvious but somewhat inefficient algorithm: Construct R, the Earley parser for the grammar. For each string S in A* (A is the alphabet for the grammar): If R recognizes S: Output S Here I

Left recursion elimination

蓝咒 提交于 2019-12-04 06:19:22
问题 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... 回答1: 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 ->

Context-Free Grammar suggestions for unary subtraction?

自闭症网瘾萝莉.ら 提交于 2019-12-04 05:23:49
问题 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. 回答1: 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 (((())))

converting context free grammar into regular expression

拥有回忆 提交于 2019-12-04 04:09:24
问题 I am currently going over CFG and saw the answer and I am not sure how they got it. How did they get it to convert into Regular Expression from CFG here? S -> aS|bX|a X -> aX|bY|a Y -> aY|a answer: R.E -> (a*(a+ba*a+ba*ba*a)) 回答1: You should learn the basic rules that I have written in my answer "constructing an equivalent regular grammar from a regular expression", those rules will help you in converting "a regular expression into right or left liner grammar" or "a right or left liner