Questions tagged [grammar]

A formal grammar is a set of production rules that describe how to form strings of valid syntax. Formal Grammars are most often used to specify the syntax of a programming language.

A formal grammar is a set of production rules that describe how to form strings of valid syntax. Formal Grammars are most often used to specify the syntax of a programming language.

A grammar is formally defined as a 4-tuple, consisting of a series of production rules, terminal symbols, nonterminal symbols, and a start symbol, which itself is a nonterminal. Any terminal cannot be a nonterminal and vice-versa.

Grammars prove very useful in parsers for programming languages. A grammar can be used to determine the syntactical correctness of a given string.

Parser generators such as JavaCC or ANTLR use a given grammar (usually one of a particular form, and free of ambiguities) to generate the parser.

3251 questions
20
votes
1 answer

Is sizeof(int()) a legal expression?

This question is inspired by Is sizeof(void()) a legal expression? but with an important difference as explained below. The expression in question is: sizeof( int() ) In the C++ grammar there appears: unary-expression: sizeof…
M.M
  • 138,810
  • 21
  • 208
  • 365
20
votes
1 answer

Correspondence between type classes and grammar levels in the Chomsky hierarchy

My question is about the Applicative and Monad type classes on the one hand, and the context-free and context-sensitive grammar levels of the Chomsky hierarchy on the other. I've heard that there's a correspondence between the type classes and the…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
19
votes
4 answers

EBNF grammar definitions for PHP?

I am wondering if someone has compiled an EBNF for PHP somewhere. I found both this site and this site. Both seem like they are incomplete, though. This question is very similar, but it's a year old. I would like to know if there has been any…
Azmisov
  • 6,493
  • 7
  • 53
  • 70
19
votes
1 answer

How does the right-shift operator work in a python print statement?

I've seen someone using "print" with ">>" to write stuffs into a file: In [7]: with open('text', 'w') as f: ...: print >> f, "Hello, world!" ...: In [8]: !type text Hello, world! How does it work? When should I use this instead of just…
Wang Dingwei
  • 4,661
  • 6
  • 32
  • 43
19
votes
4 answers

Why can't a LL grammar be left-recursive?

In the dragon book, LL grammar is defined as follows: A grammar is LL if and only if for any production A -> a|b, the following two conditions apply. FIRST(a) and FIRST(b) are disjoint. This implies that they cannot both derive EMPTY If b can…
wangshuaijie
  • 1,821
  • 3
  • 21
  • 37
18
votes
3 answers

What's the real difference between a token and a rule?

I was drawn to Raku due to its built-in grammars and figured I'd play around with it and write a simple email address parser, only problem: I couldn't get it to work. I tried countless iterations before landing on something that actually works, and…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
18
votes
2 answers

perl6 grammar , not sure about some syntax in an example

I am still learning perl6, and I am reading the example on grammar from this page: http://examples.perl6.org/categories/parsers/SimpleStrings.html ; I have read the documentations on regex multiple times, but there are still some syntax that I don't…
lisprogtor
  • 5,677
  • 11
  • 17
18
votes
10 answers

Colour instead of color?

I'm working on a game engine in C++ and I have methods like setColour and things that use British grammar. While I was thinking how C++ compilers mostly use the English language (correct me if I'm wrong) and how most APIs use American grammar,…
Jookia
  • 6,544
  • 13
  • 50
  • 60
18
votes
4 answers

How is the Python grammar used internally?

I'm trying to get a deeper understanding of how Python works, and I've been looking at the grammar shown at http://docs.python.org/3.3/reference/grammar.html. I notice it says you would have to change parsermodule.c also, but truthfully I'm just not…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
18
votes
5 answers

Combined unparser/parser generator

Is there a parser generator that also implements the inverse direction, i.e. unparsing domain objects (a.k.a. pretty-printing) from the same grammar specification? As far as I know, ANTLR does not support this.
pmf
  • 7,619
  • 4
  • 47
  • 77
18
votes
2 answers

How to represent negation in BNF?

Does BNF or ABNF support negation. That is exclude certain members of the set? I did not see any such negation operator in its syntax. For example, suppose S is the set of all alphanumeric strings that are not equal to "foo" What is the BNF for S?
Jus12
  • 17,824
  • 28
  • 99
  • 157
17
votes
2 answers

SLR(1) Parser and epsilon involved

Let's suppose I have the following grammar: S → X X → a | ϵ If that grammar wouldn't have ϵ involved, I would construct the first state like: S' → .S S → .X X → .a but what about the ϵ symbol? Should I include: X → .ϵ too? If so... when…
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
17
votes
8 answers

Learning Treetop

I'm trying to teach myself Ruby's Treetop grammar generator. I am finding that not only is the documentation woefully sparse for the "best" one out there, but that it doesn't seem to work as intuitively as I'd hoped. On a high level, I'd really…
user54650
  • 4,388
  • 2
  • 24
  • 27
17
votes
3 answers

How to read & understand C & C++ Standards and the language grammar used therein?

I often find C and C++ Standards difficult to read and understand, even the simple English sentences and their wordings give terrible experience. On the top of it, the language grammar is totally hell. I'm sure many share the same feeling, at least…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
17
votes
2 answers

What's a valid left-hand-side expression in JavaScript grammar?

Okay, we all know what the valid left-hand-side expressions are. Kind of.* But, looking at the definition from the ECMA-Script standard, I'm very confused: LeftHandSideExpression : NewExpression CallExpression Is that just an error in the…
user123444555621
  • 148,182
  • 27
  • 114
  • 126