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
1
vote
3 answers

Left-factoring a non-left-recursive grammar to make it LL(1)

I have removed left-recursion from a left-recursive grammar given to me. The original grammar is as follows: SPRIME::= Expr eof Expr::= Term | Expr + Term | Expr - Term Term::= Factor | Term * Factor | Term / Factor | Term mod Factor | Term div…
Richard Stokes
  • 3,532
  • 7
  • 41
  • 57
1
vote
1 answer

Failing to generate S=>Ba from python code given

Given S=>BaB, B=>b and null(B), the code is supposed to generate S=>BaB|aB|Ba|a and B=>b but it is not generating Ba i.e the output is S=>BaB|aB|a and B=>b which is not correct. The code can generate values correctly if terminal is not in the middle…
skyridertk
  • 103
  • 1
  • 8
1
vote
1 answer

When would a parser append its input string?

Given a parser newtype Parser a = Parser { parse :: String -> [(a,String)] } (>>=) :: Parser a -> (a -> Parser b) -> Parser b p >>= f = Parser $ \s -> concat [ parse (f a) s' | (a, s') <- parse p s ] return :: a -> Parser a return a = Parser (\s…
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
1
vote
1 answer

Why bison does not convert grammar automatically?

I am learning lexer and parser, so I am reading this classical book : flex & bison (By John Levine, Publisher: O'Reilly Media). An example is given that could not be parsed by bison : phrase : cart_animal AND CART | work_animal AND…
Stef1611
  • 1,978
  • 2
  • 11
  • 30
1
vote
1 answer

antlr grammar: Allow whitespace matching only in template string

I want to parse template strings: `Some text ${variable.name} and so on ... ${otherVariable.function(parameter)} ...` Here is my grammar: varname: VAR ; variable: varname funParameter? ('.' variable)* ; templateString: '`' (TemplateStringLiteral*…
Martin Cup
  • 2,399
  • 1
  • 21
  • 32
1
vote
2 answers

Is it normal for some C expressions allowed in its grammar, not to be allowed when compiled in practice?

I was studying the C grammar: http://www.quut.com/c/ANSI-C-grammar-y-1999.html#unary-expression There's this rule assignment_expression : conditional_expression | unary_expression assignment_operator assignment_expression ; And unary_expression :…
1
vote
2 answers

How to design a context free grammar that avoids repetition?

I am learning about context-free grammar and I would like to know how (if at all) it is possible to design a language that avoids repetition. Let's take the select statement from SQL as an example: possible: SELECT * FROM table SELECT * FROM…
User12547645
  • 6,955
  • 3
  • 38
  • 69
1
vote
1 answer

Grammar LaTeX like with mixed whitespace utf and commands

I've tried to implement a LaTeX like grammar that could allow me to parse this kind of sentence : \title{Un pré é"'§è" \VAR state \draw( 200\if{expression kjlkjé} ) bis tèr } As you can see, the \title{ } can contain several kind of items : string…
chrisb06
  • 23
  • 4
1
vote
0 answers

Is it possible to dynamically add "keywords" in VS Code syntax highlighting?

I've been playing around with syntax highlighting in VS Code and have so far been able to get a pretty good result using a custom JSON TextMate grammar. Although an extension already existed for GCC ARM Assembly, I thought it sucked, so I used the…
1
vote
1 answer

Ambiguous Regular Grammar?

Does such a thing exist? If so, could you please provide an example? Thanks.
1
vote
2 answers

Yacc parser grammar bug. int X; read separately as int X and ;

Problem Description In my yacc parser grammar, I have the following rules and corresponding actions defined (see program.y below). Parsing int X; should have the derivation type => TOK_INT and variable_list => TOK_VARIABLE, and then these match…
foobuzz
  • 75
  • 7
1
vote
1 answer

How do I figure out ANTLR grammar failure to parse input, special timestamp in logfile

INPUT: Mar 9 10:19:07 west info tmm1[17280]: 01870003:6: /Common/mysaml.app/mysaml:Common:00000000: helloasdfasdf asdfadf vgnfg GRAMMAR: grammar scratch; lines : datestamp hostname level proc msgnum module msgstring; datestamp: …
Joel D
  • 11
  • 1
1
vote
1 answer

ANTLR grammar: Add "dynamic" proximity operator

For a study project, I am using the following ANTLR grammar to parse query strings containing some simple boolean operators like AND, NOT and others: grammar SimpleBoolean; options { language = CSharp2; output = AST; } tokens { AndNode;…
Shackles
  • 1,264
  • 1
  • 19
  • 40
1
vote
1 answer

Lemon Parser REPL

I'm trying to build a Smalltalk REPL based on LanguageKit, which uses a lemon gramma. Currently the parser only supports parsing complete class definitions, but not statements outside of the method syntax. For example this gets parsed: methodName…
catlan
  • 25,100
  • 8
  • 67
  • 78
1
vote
1 answer

How can I find regular grammar for L* if we are given a grammar for language L?

Is there any general method to do so? For example, we have a general method to find grammar for L1 U L2 by adding a production S-> S1 | S2 where S1 and S2 are start symbols for grammars of L1 and L2 respectively. Thanks in advance..