context-free-grammar

Top-down parser classification

跟風遠走 提交于 2019-12-24 12:00:30
问题 I've watched this course by Alex Aiken and read through many other resources. But I'm struggling to find clear classification of top-down parsers. This document doesn't provide a clear classification either but at least gives some definitions I'll use in the post. So here is the classification I've come up so far: Backtracking VS Predictive Backtracking One solution to parsing would be to implement backtracking. Based on the information the parser currently has about the input, a decision is

Am using the pumping lemma correctly?

两盒软妹~` 提交于 2019-12-24 11:50:10
问题 I'm trying to prove that the following language is not regular via the Pumping Lemma. But I'm not truly sure if I have done it correctly. {L = a 2 n | n>= 0 } What I've done so far is the following: s = a 2 p x = a 2 i y = a 2 j z = a 2 p-i-j thus xy 2 z = a 2 p+j which means that a 2 p+j > a 2 p , making the language not regular Am I doing it correctly? or is there something I have wrong? 回答1: Not quite, you draw the correct conclusion, but the reasoning is a bit off. The Pumping lemma

Context free grammar with feature structure in Python

為{幸葍}努か 提交于 2019-12-24 10:10:31
问题 Am trying to generate sentences from a defined grammar with python, to avoid agreement problem I used feature structures, This is the code I have done so far: >>> from __future__ import print_function >>> import nltk >>> from nltk.featstruct import FeatStruct >>> from nltk import grammar, parse >>> from nltk.parse.generate import generate >>> from nltk import CFG >>> g = """ % start DP DP-> D[AGR=[NUM='sg', PERS=3, GND='m']] N[AGR=[NUM='sg', GND='m']] D[AGR=[NUM='sg', PERS=3, GND='f']] ->

How are parse trees used?

♀尐吖头ヾ 提交于 2019-12-24 09:40:38
问题 I'm currently learning about parsers. I've been watching videos and trying to write code, but I'm starting to have a hard time understanding. I thought maybe understand the motivation for a parser could help to understand how they work and how they should be built. So the goal of a parser is to take a string of tokens and create a parse tree. I can understand what a parse tree is , but I just don't see how it's used . Ultimately, a compiler uses a parse tree to create machine code, but

Why s--> ^ and A --> a ? in Context Free Grammars

爱⌒轻易说出口 提交于 2019-12-24 07:31:20
问题 I've been reading: "Tips for creating Context free grammar" post for learning purposes and I nearly understand the concept, but I don't quite understand the following. If we have: L = {a m b n | m >= n}. I understand this: S --> B B --> aBb A --> aA But what I don't understand is the concept of adding to the end of these particular values, such as: S --> B | ^ B --> aBb | A A --> aA | a Why do we add ^ (null), A , and a to the end of these lines? What do they do and why do we need them? All

Conversion to Chomsky Normal Form

我的未来我决定 提交于 2019-12-23 18:34:09
问题 I do need your help. I have these productions: 1) A--> aAb 2) A--> bAa 3) A--> ε I should apply the Chomsky Normal Form (CNF). In order to apply the above rule I should: eliminate ε producions eliminate unitary productions remove useless symbols Immediately I get stuck. The reason is that A is a nullable symbol (ε is part of its body) Of course I can't remove the A symbol. Can anyone help me to get the final solution? 回答1: As the Wikipedia notes, there are two definitions of Chomsky Normal

Expression grammar with exponentiation operator using Boost Spirit

戏子无情 提交于 2019-12-23 12:22:05
问题 I would like to add the exponentiation operator to the expression grammar provided in the Boost spirit samples. The BNF grammar is the following: (see this answer for example: "Unambiguous grammar for exponentiation operation" ) E -> E + T | E - T | T T -> T * F | T / F | X X -> X ^ Y | Y Y -> i | (E) which I translated to Boost spirit like this: template <typename Iterator> struct calculator : qi::grammar<Iterator, ascii::space_type> { calculator() : calculator::base_type(expression) { qi:

For a context free grammar, how do i convert it to an equivalent push down automaton?

痴心易碎 提交于 2019-12-23 04:15:06
问题 For a context-free grammar G over Σ = {0, 1, 2}, with start variable S: S → 0S0 | 1S1 | 2S2 | Y Y → 22 How do i turn this into an equivalent Push-Down automaton 回答1: A pushdown automaton can push symbols on top of a stack and pop them off. It can also base its transitions on the topmost stack symbol. We need to think of a mechanism that will allow us accept the right language by manipulating our stack. The language your grammar generates has the following characteristics: It has 22 in the

CFG for python-style tuples

蹲街弑〆低调 提交于 2019-12-23 00:24:47
问题 After having read for the zillionth time a question about "How do I parse HTML with Regex" on Stackoverflow, I got myself interested again in grammars, grabbed my university scripts and after a few minutes I wondered how I've ever passed my exams. As a simple (well, "simple" I expected it to be) exercise I tried to write a CFG that produces valid python tuples (for simplicity's sake only using the identifiers a , b and c ). After some good time I now came up with this: G = ( {Tuple, TupleItem

How to build parse tree?

笑着哭i 提交于 2019-12-22 14:32:23
问题 Have found C++ BNF and there next lines selection-statement: if ( condition ) statement if ( condition ) statement else statement Now trying to write parser. Need to build parse tree. On input i have BNF and source file. But i'm stucked in how i can point my parser what if condition evaluated to true, then it need to execute first statement otherwise else block? Thanks. 回答1: Conditional statements have a simple recursive structure. The corresponding recursive descent parser has a similarly