C++ Grammars

Started by
9 comments, last by daviangel 12 years, 11 months ago
I am new to C++, but have done other work in Java and Basic. Anyways, my question is "What is a Grammar in C++? And how do I use one?" In my book I'm reading "Programming Principals and Practice using C++" I need to know what a Grammar is. It explains it very vaguely, and I just don't understand it. Can you please help?

Thanks
Jesse
Advertisement
In general usage, the term "grammar" is a concept that is rather outside of any particular programming language. Specifically, a grammar is a formal specification of how a language is parsed by a computer - that is, how the computer breaks down the elements of the language to extract the intended meaning. It's basically the same concept as the grammar of a spoken or written language in that sense.

C++ has no concept of "grammar" in that you cannot directly specify a grammar as a native C++ concept. C++ has a grammar (i.e. a formal syntax) but does not "contain" the concept of specifying other grammars. There are notable extensions to the language via mechanisms like operator overloading, which allow you to specify grammars using C++ syntax (boost::spirit comes to mind); but this is really a sort of metaprogramming trick and has no relationship to the C++ language itself and its ability to talk about language grammars directly.


I'm curious how the book you reference uses the term in context, because it sounds to me like there's a missing piece of the puzzle in your question.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

A grammar for a programming language is the set of rules that define how you are allowed to write each piece of the source code. It's like:

"A function is written: (qualifiers) return_type function_name ( parameter_list ) function_body"

"A function_body is made up of one or more statements."

"A statement can be one of: An assignment to a variable, a call to a function, a conditional or loop statement, or a return statement."

"A conditional statement can be an 'if' or 'switch' statement"

"An 'if' statement is written: if ( conditional_expression ) statement, with an optional else_statement"


These individual things are pretty obvious if you've been programming for a while. They're just called a "grammar" for the language. Sometimes, rarely, you can read through a grammar and discover things you didn't know were actually allowed in the language, and use that to your advantage.


The formally-written grammar itself is really useful for compiler-writers (they use tools that take the raw grammar as input and generate a parser as output). Normally you can learn new languages without having to go read the dry, boring grammar itself.
<br />In general usage, the term &quot;grammar&quot; is a concept that is rather outside of any particular programming language. Specifically, a grammar is a formal specification of how a language is <i>parsed</i> by a computer - that is, how the computer breaks down the elements of the language to extract the intended meaning. It's basically the same concept as the grammar of a spoken or written language in that sense.<br /><br />C++ has no concept of &quot;grammar&quot; in that you cannot directly specify a grammar as a native C++ concept. C++ <i>has a </i>grammar (i.e. a formal syntax) but does not &quot;contain&quot; the concept of specifying other grammars. There are notable extensions to the language via mechanisms like operator overloading, which allow you to specify grammars using C++ syntax (boost::spirit comes to mind); but this is really a sort of metaprogramming trick and has no relationship to the C++ language itself and its ability to talk about language grammars directly.<br /><br /><br />I'm curious how the book you reference uses the term in context, because it sounds to me like there's a missing piece of the puzzle in your question.<br />
<br /><br /><br />

The book also refers to "Grammars" as a Parser and a Syntax Analyzer. It is in a chapter about making a calculator. We use the grammar to decide what the order of operations is. Like, it checks the expression for a '*' or '/' then does that, followed by addition and subtraction. I don't know if this is a good explanation or not, but I tried. If you want, you can look up the book. The part on Grammars is in Chapter 6.
Yeah, it seems like that book is going over building your own parser. Order of operations can be done via fancy tricks in your grammar.
It explains it very vaguely, and I just don't understand it[/quote]
The grammar refers to formal grammar. It's one of fundamental computer science concepts.Also the basis for regular expressions.

This used to be part of just about every decent programming book, but it's rarely mentioned these days. It's only useful when doing certain types of parsing which are quite uncommon in typical applications. Most of newer dynamic languages can also solve such problems using first-class regex expressions and dynamic code evaluation. For C and C+, tools like Flex/Byson are typically used to generate lexers or parsers from grammar definitions. Languages like Ruby avoid this and allow one to write DSL (domain-specific languages) directly. JavaScript is also convenient for tasks like this.

Unfortunately, there is no quick answer as to what grammar is, it's a bunch of math. If you want a simpler explanation, look up tutorials on regular expressions. Same concept, just a limited and perhaps more useful subset.
I'm still kind of unclear on how to implement them in C++.
A grammar is just a collection of notations; there's nothing to "implement." It's akin to saying you want to implement equations in C++; equations are just a way of writing certain information. Grammars are no different - they're just a tool for communicating how something is parsed.

Implementing the parser is a different matter; there is an entire field of computer science research dedicated to parsing theory. There are many ways to parse things, mostly equivalent in power but with varying tradeoffs in complexity, ease of implementation, performance in certain cases, and so on. It sounds like your book is walking you through the process of building a very simple parser; so that should be your reference point.

If you have specific questions about the book's instructions, or if you want particular details on parsing itself, please ask those questions instead of leaving vague "I don't understand X" posts. It's extremely hard to summarize such a broad field as parsing theory on an internet forum, so we need some guidance from you as to what exactly you're having trouble with.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

C++ Grammer.

cppgrammar.png
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

C++ Grammer.

Wow, great find. Thanks for posting!

This topic is closed to new replies.

Advertisement