Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [grammar]

The tag has no usage guidance.

-4
votes
1answer
41 views

LL(K) grammer generation for postfix expressions [closed]

I have a problem in formulating LL(K) grammar for this postfix expression problem, given (4 3 / 2 * 4 5 / +) as an input must output 52/12
0
votes
2answers
92 views

Understanding Context Free Grammar using a simple C code

I'm trying to understand the difference between terminal and non-terminal values in a real language. I wasn't able to find enough examples on real language CFGs on the internet, most examples are ...
2
votes
1answer
126 views

How to say this statement correctly?

When reading code, how do you say the following statement? (It was taken from some C# code, could be many languages, though). List<string> Do you say: List of type string? or List of ...
-2
votes
3answers
117 views

Parsing Complex Context-Sensitive Text

I am trying to learn how to handle complex context-sensitivity in parsing. Say you have this simple example of code: var x = 10 var y = x + 10 var z = y + 10 var a = z + 10 var b = a + 10 var c = b + ...
0
votes
1answer
225 views

Do any programming languages let you use other languages without restriction within them?

This may be a stupid question, and it would certainly take one Hell of a lexer, but do any extant programming languages allow you to do something like: c# (1.2) { // c# code } Perl (5) { # ...
10
votes
2answers
332 views

Why does Forth's flexibility make a grammar inappropriate for it?

I've recently undertaken the task of writing a stack-based programming language. Before I started designing my language however, I thought it'd be a good idea to read and experiment with existing ...
0
votes
1answer
312 views

Grammar by example

Is there an automated approach to generate a grammar (which could be used later in a compiler tool such as ANTLR or similar) from given examples of a language? With more detail: assumed a technical ...
0
votes
1answer
952 views

Is repetition expressed in Backus–Naur form by recursive production definitions?

When reading grammars defined using Backus–Naur form (BNF), I've noticed that the grammars never seem to use an explicit repetition symbol, such as * or +. This is opposed to Extend Backus–Naur form ...
0
votes
0answers
54 views

Is there a name for this grammar transform (or a more general form of it)?

I'm working on removing left recursion from a grammar, and of course started with the algorithm described on Wikipedia. As advertised, it unfortunately exploded into a much larger grammar that was ...
11
votes
1answer
1k views

What is the procedure that is followed when writing a lexer based upon a grammar?

While reading through an answer to the question Clarification about Grammars , Lexers and Parsers, the answer stated that: [...] a BNF grammar contains all the rules you need for lexical analysis ...
2
votes
1answer
1k views

Understanding abstract syntax trees in compilers

I have attached the problem below with the answer. My problem is that I can't understand it. Can you provide an overall explanation in detail about parse trees and ETF grammar by deriving the first ...
0
votes
0answers
97 views

LALR parsing (with Ply) non-deterministic and sometimes fails

I have written a parser using Python Ply library. While doing tests with an input file I have noticed a strange behaviour : sometimes the input is correctly parsed with no error and sometimes, there ...
0
votes
1answer
81 views

Grammar design preference for recursion

I would like to know if one of the two following equivalent grammars (since they can produce the same rules) is preferred (and why). For instance the second grammar is more concise but is it a good ...
3
votes
1answer
283 views

Javascript / Ecmascript language grammar disambiguation

I'm working on the design of a compiler for a language in which I have to use curly brace for two different purposes. I am currently stuck in writing a non-ambiguous grammar but I realized that some ...
0
votes
1answer
241 views

Is it possible to parse my grammar with multi-line productions without backtracking?

I'm playing around with creating a parser in PHP for my own flavor of BNF, to match strings against grammar in this BNF variant. It's still a work in progress and subject to change (I may even end up ...
0
votes
1answer
630 views

Is an AST enough to build any translator? [closed]

Note: In my ignorance of the difference between Programmers vs StackOverflow sites (which I know now), I had posted this question on StackOverflow earlier. What I'm looking for is some elaboration, ...
2
votes
1answer
80 views

Inferring programmatically terminals and non-terminals in context-free grammars?

I'm trying to compute the FIRST and FOLLOW sets of a CFG (using Python). To do this I need to recognise terminals and non-terminals. I can (as a human) read them, but I was wondering if there was ...
0
votes
1answer
196 views

Is it a reasonable task to automatically convert ANTLR grammars to my own format?

I was working on my own parser generator tool in Clojure and then I realised I want to test it with some real programming languages. I can find a lot of existing language grammars only in antlr v4 ...
9
votes
3answers
666 views

Does it make sense to use “ys” instead of “ies” in identifiers to ease find-and-replace functionality? [closed]

Although grammatically incorrect, when writing identifiers for functions, variables etc. does it make sense to simply append an "s" to plurals of words ending in Y? My reason for this would be that if ...
2
votes
2answers
955 views

Incorporating functions into a Shunting-Yard algorithm implementation

tl;dr What would be a simple way of incorporating functions into a Shunting-Yard algorithm implementation? If only expressions like function(arg1, arg2, arg3) were allowed (where function is some ...
2
votes
2answers
669 views

“Hand written” recursive descent parser with “catch all” rule

I'm trying to write a (scannerless) recursive descent parser with a "catch all" rule for the following "Mustache template" grammar (simplified here): content : (variable_tag | section_tag | ...
0
votes
1answer
189 views

Injectable, Poppable Stack

Background A project involves converting a relational expression map (rxm) to an XML/SQL statement. The map resembles: root > people, # "root" keyword starts the document ...
1
vote
1answer
60 views

Can table of content be parsed using some formal grammar?

A table of content can look like: Preface Table of Content Chapter 1 ... 1.1 ... 1.1.1 ... 1.1.2 .... 1.2 ... Summary Exercises Chapter 2 ... ... Appendix ... A ... A.1 ... A.2 ... B ... References ...
39
votes
12answers
7k views

Why do most programming languages have special keyword or syntax for declaring functions? [closed]

Most programming languages (both dynamically and statically typed languages) have special keyword and/or syntax that looks much different than declaring variables for declaring functions. I see ...
55
votes
5answers
13k views

What does “context-free” mean in the term “context-free grammar”?

Given the amount of material that tries to explain what a context-free grammar (CFG) is, I found it surprising that very few (in my sample, less than 1 in 20) give an explanation on why such grammars ...
0
votes
1answer
139 views

About grammar, Chomsky hierarchy and obj file format [closed]

Considering the Wavefront obj file format where this syntax fits in the Chomsky hierarchy and why ? I'm also not sure if PEG grammars are being considered in the kind of classification made by Chomsky ...
7
votes
3answers
1k views

Clarification about Grammars , Lexers and Parsers

Background info (May Skip): I am working on a task we have been set at uni in which we have to design a grammar for a DSL we have been provided with. The grammar must be in BNF or EBNF. As well as ...
2
votes
1answer
281 views

Resources for understanding a programming language and its specifications and grammar deeply [closed]

I'm a computer engineer and all the courses we have had about programming and computer languages were an introduction to C/Java and OOP. Now I feel like reading Java language specification to learn a ...
2
votes
0answers
320 views

Implementing semantic actions in table driven predictive parsing

When doing table driven predictive parsing on an LL(1) grammar, (as explained in good detail here for example), how can we augment the algorithm to allow processing of semantic actions while doing the ...
2
votes
1answer
3k views

How to write a syntax checker [closed]

How is a syntax checker for a programming language written? I am guessing a grammar has to be written first. How to then proceed? Motivation I'm using a programming language (Apex for Salesforce) ...
11
votes
2answers
3k views

Haskell memory efficiency - which is the better approach?

We are implementing a matrix compression library based on a modified two dimensional grammar syntax. Now we have two approaches for our data types -which one will be better in case of memory usage? (...
1
vote
1answer
584 views

Should I use a formal grammar for my interpreted scripting language

I have a scripting engine I just published as an open source project. It's been sitting on my harddrive waiting for about a year. My engine of course isn't complete in any way, but it does work for ...
14
votes
3answers
2k views

Object-Oriented Programming: Why “Oriented”?

I am most of the way through my games programming degree. This is not a computer science degree, so a lot of the theory is eschewed in favour of practical portfolio building and what I see as JIT ...
2
votes
2answers
3k views

Convert grammar into an LL(1) grammar which recognises the same language

I have the following sample question for a compilers exam and wanted to check my solution. Convert the following grammar into an LL(1) grammar which recognises the same language: E -> E + T E -...
5
votes
3answers
600 views

Constructing a Finite State Automaton

I have an exam question that I am unsure of the answer. The question is: In organisation X valid user names have the following structure. The user name can be either the employee’s name followed ...
3
votes
1answer
287 views

Showing a grammar is ambiguous

I have the following question taken from a compilers course exam: Show that the following grammar is ambiguous. S = XcY X = a Y = b | Z Z = bW W = d | ϵ I drew the following tree: Am I correct in ...
9
votes
3answers
2k views

What is a real-world use case of using a Chomsky Type-I (context-sensitive) grammar

I have been having some fun lately exploring the development of language parsers in the context of how they fit into the Chomsky Hierarchy. What is a good real-world (ie not theoretical) example of a ...
1
vote
2answers
269 views

Good principles to explain the use of data objects

I'm in a little need of names clarification. First a brief description of my position. I prepare as debutant giving a junior developer job descriptions, which also provided another perspective on how ...
3
votes
1answer
274 views

How to support mixed grammars?

I am currently writing a grammar for a programming language (PowerBuilder) and I am also collecting documentation about parsing and compilers creation. Now I would like to add in my grammar (that ...
66
votes
10answers
4k views

Is it worth making a commit solely to resolve non-critical typos?

If I come across a non-critical typo in code (say, an errant apostrophe in a print(error) statement), is it worth making a commit to resolve that error, or should it simply be left alone? ...
8
votes
4answers
1k views

How are comments expressed in programming language grammars?

I'm learning how to build parsers using grammars, but I got stuck trying to express comments, because they can appear almost anywhere. This indicates that comments can be stripped from the token ...
40
votes
7answers
44k views

Why the static data members have to be defined outside the class separately in C++ (unlike Java)?

class A { static int foo () {} // ok static int x; // <--- needed to be defined separately in .cpp file }; I don't see a need of having A::x defined separately in a .cpp file (or same file for ...
19
votes
4answers
38k views

How can I extract words from a sentence and determine what part of speech each is? [closed]

I want to write something that takes a sentence and identifies each word it contains and defines what part of speech each word is. For example Hello World, I am a sentence would return this ...
5
votes
1answer
361 views

Visualizing a CUP grammar

I am implementing a grammar in CUP, and it would often be helpful to see everything at a glance. This could also be useful in finding errors quickly. Are there any programs out there that will ...
0
votes
3answers
1k views

Insert grammatical mistakes in a correct sentence

I'd like to insert grammatical mistakes (not typo) in a correct sentence, to make a small game. For instance: My name is John -> My name are John He leaves the room -> He leave the room I only ...
3
votes
3answers
514 views

What makes one language any better than another when both are designed for the same goals? [closed]

I'm in the process of creating a grammar for a scripting language but as I'm working on it I started to wonder what makes a language good in the first place. I know the goals for my script but there ...
2
votes
1answer
371 views

Best way to parse optional grammar rules?

I started to write a hand written parser just for fun. For simple rules it is working, but I have problem with the optional grammar rules. I marked the optional rules with a question mark. Here is a ...
14
votes
3answers
11k views

What is the difference between syntax and grammar?

I understand the difference between syntax and semantics - Syntax: how the symbols are combined to form a valid expression or statement. Semantics: the meaning of those symbols that form an ...
12
votes
3answers
5k views

How should I specify a grammar for a parser?

I have been programming for many years, but one task that still takes me inordinately long is to specify a grammar for a parser, and even after this excessive effort, I'm never sure that the grammar I'...
7
votes
1answer
923 views

Using grammars to extract bibliographic citations

My task lays in the area of text mining / labelling, more particular extraction of bibliographic citations (references) from the plain text, as shown here: Basically, for the input text the algorithm ...