context-free-grammar

Identity expression, factor, and term?

我只是一个虾纸丫 提交于 2019-12-07 09:51:48
问题 I am learning context-free grammar, and I don't understand how to identity expression, factor and term in a programming language like C or C++. Suppose we have an assignment statement, id := E , where E is any arithmetic expression. What is a term? What is an expression? and What is a factor in an actual piece of code? We can have int i = 3, j = 14 int i = 3 + j * 14; Thank you very much. 回答1: The "factor", "term" and "expression" concepts come from math and don't really have to do with

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

北慕城南 提交于 2019-12-07 07:55:29
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 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 middle It is a palindrome over {0, 1, 2} . That is, it reads the same forwards as backwards. We need to

Is csv format regular grammar or context-free grammar?

£可爱£侵袭症+ 提交于 2019-12-06 16:55:56
I am currently writing a csv parser. The definition of csv format is given by RFC4180 which is defined by ABNF. So the definition of csv is absolutely a contex-free grammar. However, I would like to know if csv is regular grammar? So that I could parse it with just a finite state machine. Furthermore, if it is exactly a regular grammar and can be parsed by finite state machine, does that mean it can be also parsed by regular expression? I don't have any formal theory available to verify this, but I'm pretty sure CSV files can reliably be parsed with regular expressions. It's probably best to

Left recursion elimination in an LL1 grammar

孤街醉人 提交于 2019-12-06 16:06:20
I'm trying to eliminate left recursion from the following extract of a grammar - expression := fragment ( ( + | - | * | / ) fragment )* fragment := identifier | number | ( + | - ) fragment | expression The issue is that expression can go to fragment, can go to expression. I've tried a bunch of ways to eliminate it, some look like they work (in JavaCC) but I'm a)unsure of their correctness, and b) pretty sure I've broken associativity by changing the structure of the grammar. I'm pretty sure I need an expression', and have fragment := identifier | number | ( + | - ) fragment | expression

CFG for python-style tuples

泄露秘密 提交于 2019-12-06 13:13:58
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, Id}, {“a”, “b”, “c”, “,”, “(“, “)”}, P, Tuple) Being P: Tuple → “(“ TupleItem “)” Tuple → “(“

How to build parse tree?

拈花ヽ惹草 提交于 2019-12-06 07:02:10
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. Conditional statements have a simple recursive structure. The corresponding recursive descent parser has a similarly simple recursive structure. Abstractly, the interior conditionals are parsed as follows: <cond> -> if

Construct Context free Grammar

大兔子大兔子 提交于 2019-12-06 04:42:16
How can I construct a context free grammar for the following language: L = {a^l b^m c^n d^p | l+n==m+p; l,m,n,p >=1} I started by attempting: S -> abcd | aAbBcd | abcCdD | aAbcdD | AabBcCd and then A = something else... but I couldn't get this working. . I was wondering how can we remember how many c's shud be increased for the no. of b's increased? For example: string : abbccd The grammar is : S1 -> a S1 d | S2 S2 -> S3 S4 S3 -> a S3 b | epsilon S4 -> S5 S6 S5 -> b S5 c | epsilon S6 -> c S6 d | epsilon Rule 1 adds equal number of a's and d's. Rule 3 adds equal number of a's and b's. Rule 5

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

点点圈 提交于 2019-12-06 03:59:39
问题 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.) 回答1: Here's an obvious but somewhat inefficient algorithm: Construct R, the Earley parser for the

Free-form text with custom SRGS based Grammar

末鹿安然 提交于 2019-12-05 20:53:22
I am trying to develop a Voice based application that would accept user input as speech and perform some actions based on the input. This is my first ever venture into this technology and I am learning while developing it. I am using Microsoft SAPI shipped with dotnet 4 to recognize speech. So far, I have learned about the two types of modes it supports. Speech recognition (SR) has two modes of operation: Dictation mode — an unconstrained, free-form speech interpretation mode that uses a built-in grammar provided by the recognizer for a specific language. This is the default recognizer.

Grammar for expressions which disallows outer parentheses

老子叫甜甜 提交于 2019-12-05 20:22:58
I have the following grammar for expressions involving binary operators (| ^ & << >> + - * /): expression : expression BITWISE_OR xor_expression | xor_expression xor_expression : xor_expression BITWISE_XOR and_expression | and_expression and_expression : and_expression BITWISE_AND shift_expression | shift_expression shift_expression : shift_expression LEFT_SHIFT arith_expression | shift_expression RIGHT_SHIFT arith_expression | arith_expression arith_expression : arith_expression PLUS term | arith_expression MINUS term | term term : term TIMES factor | term DIVIDE factor | factor factor :