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
30
votes
5 answers

How to define a grammar for a programming language

How to define a grammar (context-free) for a new programming language (imperative programming language) that you want to design from scratch. In other words: How do you proceed when you want to create a new programming language from scratch.
30
votes
6 answers

if statement in R can only have one line?

I was trying a tiny code with if statement, although it is very simple,but there is something I really confused here is the code n<-857 while(n!=1){ if(n<=0) print("please input a positive integer") else if(n%%2==0) n<-n/2 print(n) else …
TOPMAD
  • 621
  • 2
  • 8
  • 14
30
votes
2 answers

Grammatical inference of regular expressions for given finite list of representative strings?

I'm working on analyzing a large public dataset with lots of verbose human-readable strings that were clearly generated by some regular (in the formal language theory sense) grammar. It's not too hard to look at sets of these strings one by one to…
Stephen Lin
  • 5,470
  • 26
  • 48
29
votes
2 answers

What are terminal and nonterminal symbols?

I am reading Rebol Wikipedia page. "Parse expressions are written in the parse dialect, which, like the do dialect, is an expression-oriented sublanguage of the data exchange dialect. Unlike the do dialect, the parse dialect uses keywords…
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
29
votes
3 answers

Regex BNF Grammar

Is there any BNF grammar for regular expression?
Julien Grenier
  • 3,364
  • 2
  • 30
  • 43
29
votes
6 answers

Looking for a Complete Delphi (object pascal) syntax

I need a complete Object Pascal syntax (preferably Delphi 2009). Some of the syntax is given by the help files, but not all information is provided. So I started collecting loose bits of information. Recently I added these to a more or less complete…
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
29
votes
2 answers

How do Java, C++, C#, etc. get around this particular syntactic ambiguity with < and >?

I used to think C++ was the "weird" one with all the ambiguities with < and >, but after trying to implement a parser I think I found an example which breaks just about every language that uses < and > for generic types: f(g(j)); This could…
user541686
  • 205,094
  • 128
  • 528
  • 886
28
votes
5 answers

Where can I find a formal grammar for MATLAB?

I would like to write a lexer generator to convert a basic subset of the MATLAB language to C#, C++, etc. To help me do this, I would like to find a document containing the formal grammar for MATLAB. Having spent a bit of time investigating this, it…
Dave Maff
  • 798
  • 8
  • 12
28
votes
3 answers

What are the rules of semicolon inference?

Kotlin provides “semicolon inference”: syntactically, subsentences (e.g., statements, declarations etc) are separated by the pseudo-token SEMI, which stands for “semicolon or newline”. In most cases, there’s no need for semicolons in Kotlin…
Malcolm
  • 41,014
  • 11
  • 68
  • 91
27
votes
3 answers

Combining a Tokenizer into a Grammar and Parser with NLTK

I am making my way through the NLTK book and I can't seem to do something that would appear to be a natural first step for building a decent grammar. My goal is to build a grammar for a particular text corpus. (Initial question: Should I even try…
speedplane
  • 15,673
  • 16
  • 86
  • 138
27
votes
2 answers

Converting ambiguous grammar to unambiguous

I did not understand how a unambiguous grammar is derived from a ambiguous grammar? Consider the example on site: Example. How was the grammar derived is confusing to me. Can anyone please guide me ?
name_masked
  • 9,544
  • 41
  • 118
  • 172
27
votes
4 answers

Where can I find C# 3.0 grammar?

I'm planning to write a C# 3.0 compiler in C#. Where can I get the grammar for parser generation? Preferably one that works with ANTLR v3 without modification.
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
26
votes
5 answers

Repository of BNF Grammars?

Is there a place I can find Backus–Naur Form or BNF grammars for popular languages? Whenever I do a search I don't turn up much, but I figure they must be published somewhere. I'm most interested in seeing one for Objective-C and maybe MySQL.
Greg
  • 45,306
  • 89
  • 231
  • 297
26
votes
5 answers

Which type of quotes we should use in css background url ("....")? Single, double or no quote needed?

this background:url(http://url); this background:url("http://url"); or this background:url('http://url');
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
26
votes
3 answers

Objective-C standards document

I'm a C and C++ programmer trying to get started with Objective-C. I'm really bewildered, though, by the apparent total absence of a standards document for the language and standard library. I can understand that there's no ISO standard, but is…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159