C++ Grammars

Started by
9 comments, last by daviangel 12 years, 11 months ago

[quote name='ApochPiQ' timestamp='1307341602' post='4819989']<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.
[/quote]
Yes this is the Stroustroup Swan book right.
I read this when it first came out and yeah even though he aims the book at beginners I think he loses his audience when he starts talking about grammars and doesn't really explain them.
I'm sure it doesn't make any sense to you right now unless you've run into this topic before hence your post here but trust me it will all make sense after he completes his scientific calculator example in C++ with the ability to store/define new symbols, etc.
Basically what he is doing is using this

Recursive descent parser
to make the calculator the most robust and expandible as possible according to his thinking.
I really wouldn't expect many beginners to get most of this. Actually, I send Dr. Stroustroup some corrections on the errors in the grammar in this chapter/chapters since it's pretty "dense" for a beginners book!
But basically he's got to define the grammar i.e. how expressions will be operated on before he starts writing the code to evaluate those expressions and if the input doesn't conform to the grammar rules mentioned in the book it will be treated as an error.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement