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
23
votes
7 answers

How to determine whether a language is LL(1) LR(0) SLR(1)

Is there a simple way to determine whether a grammar is LL(1), LR(0), SLR(1)... just from looking on the grammar without doing any complex analysis? For instance: To decide whether a BNF Grammar is LL(1) you have to calculate First and Follow sets -…
Chris
  • 9,209
  • 16
  • 58
  • 74
23
votes
7 answers

Efficient Context-Free Grammar parser, preferably Python-friendly

I am in need of parsing a small subset of English for one of my project, described as a context-free grammar with (1-level) feature structures (example) and I need to do it efficiently . Right now I'm using NLTK's parser which produces the right…
Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
22
votes
4 answers

Parser Combinators, separating grammar and AST construction

I'm writing a simple functional programming language in Scala using the parser-combinators library. The syntax is specified here: https://github.com/hejfelix/Frase/blob/master/src/main/scala/it/vigtig/lambda/ParserLike.scala There is one thing which…
Felix
  • 8,385
  • 10
  • 40
  • 59
22
votes
3 answers

How to implement Backus-Naur Form in Python

I know there are some vaguely similar questions already relating to BNF (Backus-Naur Form) grammars in Python, but none of them help me much in terms of my application. I have multiple BNFs that I need to write code for. The code should be able to…
Jakemmarsh
  • 4,601
  • 12
  • 43
  • 70
22
votes
2 answers

Left-Linear and Right-Linear Grammars

I need help with constructing a left-linear and right-linear grammar for the languages below? a) (0+1)*00(0+1)* b) 0*(1(0+1))* c) (((01+10)*11)*00)* For a) I have the following: Left-linear S --> B00 | S11 B --> B0|B1|011 Right-linear S --> 00B…
22
votes
3 answers

How can I incorporate ternary operators into a precedence climbing algorithm?

I followed the explanation given in the "Precedence climbing" section on this webpage to implement an arithmetic evaluator using the precedence climbing algorithm with various unary prefix and binary infix operators. I would also like to include…
Matt
  • 21,026
  • 18
  • 63
  • 115
21
votes
9 answers

How do I generate sentences from a formal grammar?

What's a common way of generating sentences from a grammar? I want an algorithm that's sort of the opposite of a parser. That is, given a formal context-free grammar (say LL), I want to generate an arbitrary sentence that conforms to that grammar. I…
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
21
votes
7 answers

Lisp grammar in yacc

I am trying to build a Lisp grammar. Easy, right? Apparently not. I present these inputs and receive errors... ( 1 1) 23 23 23 ui ui This is the grammar... %% sexpr: atom {printf("matched sexpr\n");} | list ; list: '('…
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
21
votes
3 answers

Where can I find a formal grammar for the Perl programming language?

I understand that the Perl syntax is ambiguous and that its disambiguation is non-trivial (sometimes involving execution of code during the compile phase). Regardless, does Perl have a formal grammar (albeit ambiguous and/or context-sensitive)?
Adam Paynter
  • 46,244
  • 33
  • 149
  • 164
21
votes
4 answers

History of trailing comma in programming language grammars

Many programming languages allow trailing commas in their grammar following the last item in a list. Supposedly this was done to simplify automatic code generation, which is understandable. As an example, the following is a perfectly legal array…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
21
votes
12 answers

Why do a lot of programming languages put the type *after* the variable name?

I just came across this question in the Go FAQ, and it reminded me of something that's been bugging me for a while. Unfortunately, I don't really see what the answer is getting at. It seems like almost every non C-like language puts the type after…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
21
votes
2 answers

Haskell Precedence: Lambda and operator

I found precedence and associativity is a big obstacle for me to understand what the grammar is trying to express at first glance to haskell code. For example, blockyPlain :: Monad m => m t -> m t1 -> m (t, t1) blockyPlain xs ys = xs >>= \x -> ys…
ning
  • 731
  • 1
  • 4
  • 15
21
votes
2 answers

What makes Ometa special?

Ometa is "a new object-oriented language for pattern matching." I've encountered pattern matching in languages like Oz tools to parse grammars like Lexx/Yacc or Pyparsing before. Despite looking at example code, reading discussions, and talking to…
Brian
  • 25,523
  • 18
  • 82
  • 173
20
votes
5 answers

Haskell - How to best to represent a programming language's grammar?

I've been looking at Haskell and I'd quite like to write a compiler in it (as a learning exercise), since a lot of its innate features can be readily applied to a compiler (particularly a recursive descent compiler). What I can't quite get my head…
Peter
  • 435
  • 1
  • 4
  • 9
20
votes
8 answers

How to get all captures of subgroup matches with preg_match_all()?

Update/Note: I think what I'm probably looking for is to get the captures of a group in PHP. Referenced: PCRE regular expressions using named pattern subroutines. (Read carefully:) I have a string that contains a variable number of segments…
hakre
  • 193,403
  • 52
  • 435
  • 836