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
2 answers

How to remove Left recursive in this ANTLR grammar?

I am trying to parse CSP(Communicating Sequential Processes) CSP Reference Manual. I have defined following grammar rules. assignment : IDENT '=' processExpression ; processExpression : ( STOP | SKIP | chaos |…
Tasawer Khan
  • 5,994
  • 7
  • 46
  • 69
1
vote
1 answer

How to parse invalid JSON using Lark?

Let's start by considering a simple json parser using lark: import sys from lark import Lark, Transformer, v_args json_grammar = r""" ?start: value ?value: object | array | string | SIGNED_NUMBER ->…
BPL
  • 9,632
  • 9
  • 59
  • 117
1
vote
1 answer

How can I change which grammar gets used in Atom for ERB?

Atom recently stopped highlighting any embedded ruby inside my .html.erb files, so now they look like this: However, if I change the grammar being used for the editor file to HTML (Rails) instead of ERB, everything goes back to being highlighted…
Josh Hadik
  • 403
  • 5
  • 16
1
vote
1 answer

how the order in this recursive rule does not give the same result?

can anyone tell me what's the difference between the following two rules (Notice the order)? the first which doesn't work without => "[" "]" without | "[" "]" with => "[" INDEX "]" with | "[" INDEX "]" array => ID with | ID without | ID with…
1
vote
0 answers

CFG for constituent tree of As Bs and as and bs

I have the following constituent tree, and I am confused on how to find the context-free grammar as I am not used to these notations of As and Bs. S / \ …
1
vote
1 answer

ANTLR4 Unexpected Parse Behavior

I am trying to build a new language with ANTLR, and I have run into a problem. I am trying to support numerical expressions and mathematical operations on numbers(pretty important I reckon), but the parser doesn't seem to be acting how I expect.…
Cubemaster
  • 507
  • 1
  • 6
  • 20
1
vote
1 answer

Calculating first and follow sets of this grammar

I am doing exam questions for compilers. The question is to find the first and follow sets of the following grammar: S → uBDz B → Bv | w D → EF E → y | e F → x | e This is what I got when I calculated the First set: First S u,v,w,y,x,z,e B …
user7302428
1
vote
1 answer

Unable to catch ANTLR4 Syntax errors

I am trying to catch syntax errors in ANTLR4 before I visit the ParseTree. Simplified, my grammar for declaring a double is like this: Grammar for double type (parser rule): declare_double: DOUBLE_TYPE WHITESPACE ID WHITESPACE(EQUALS WHITESPACE…
Niek Jonkman
  • 1,014
  • 2
  • 13
  • 31
1
vote
1 answer

Extend Antlr grammar file with lists

I have the following assignment about extending an Antlr grammar. What I've tried is: I am not sure whether this is the correct solution or not. Can anyone guide me in the right direction?
Boris Grunwald
  • 2,602
  • 3
  • 20
  • 36
1
vote
1 answer

Grammar with Epsilon or Lambda

So I have a set of grammar S -> X Y X -> a X X -> Y -> b Z -> a Z Z -> a My only confusing with this grammar is that 2nd Production for X There is nothing there. Is that the equivalent of using Epsilon ε, or Lamda λ I am assuming it is merely a…
billybob2
  • 681
  • 1
  • 8
  • 17
1
vote
1 answer

idea grammar-kit recoverWhile beaks the parser on the first element in list

I have a bnf grammar: { tokens = [ COLON = ":" space=' ' word = 'regexp:[^\r\n\s\t@\$\{\}\(\)\|\#:<>]+' nl = 'regexp:\r|\n|(\r\n)' ] } root ::= nlsp book_keyword COLON [space] book_title sections book_keyword…
1
vote
0 answers

What does "comp_for" mean in Python grammar?

I'm studying Python's grammar from the official documentation, but I couldn't figure out what the comp_for production does. The production is called from the following productions: testlist_comp, dictorsetmaker, argument, and comp_iter. What does…
Felipe
  • 21
  • 4
1
vote
1 answer

LL1 grammar for arithmetic expressions and assignments

I would like to design an LL1 grammar for arithmetic equations and variable assignments. I began with this grammar: I have a nonambiguous grammar for arithmetic expressions: E → T E’ E’ → | + E T → id T’ T’ → | * T However, I'm not sure how to…
maddie
  • 1,854
  • 4
  • 30
  • 66
1
vote
1 answer

Q: ANTLR 4 Grammar recognition of whole odd value not only the last digit

I'm trying to make grammar for the calculator, however it have to be working only for odd numbers. For example it works like that: If I put 123 the result is 123. If I put 1234 the result is 123, and the token recognition error at: 4 but should be…
Cevz
  • 11
  • 3
1
vote
1 answer

ANTLR4 Correctly continuing to parse sections after error

I'm trying to write some tooling (validation/possibly autocomplete) for a SQL-esk query language. However, parser is tokenizing invalid/incomplete inputs in a way that is making it more difficult to work with. I've reduce my scenario to its…
NSjonas
  • 10,693
  • 9
  • 66
  • 92
1 2 3
99
100