Sign in to follow this  
stein102

C++ Grammars

Recommended Posts

stein102    556
[size="3"]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[/size]

Share this post


Link to post
Share on other sites
ApochPiQ    23061
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 [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.

C++ has no concept of "grammar" 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 "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.

Share this post


Link to post
Share on other sites
Nypyren    12074
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.

Share this post


Link to post
Share on other sites
stein102    556
[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 />[/quote]<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.

Share this post


Link to post
Share on other sites
Telastyn    3777
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.

Share this post


Link to post
Share on other sites
Antheus    2409
[quote] It explains it very vaguely, and I just don't understand it[/quote]
The grammar refers to [url="http://en.wikipedia.org/wiki/Formal_grammar"]formal grammar[/url]. 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.

Share this post


Link to post
Share on other sites
ApochPiQ    23061
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 [i]parser[/i] 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 [i]you[/i] as to what exactly you're having trouble with.

Share this post


Link to post
Share on other sites
kloffy    1318
[quote name='D.Chhetri' timestamp='1307892362' post='4822407']
C++ Grammer.[/quote]
Wow, great find. Thanks for posting!

Share this post


Link to post
Share on other sites
daviangel    604
[quote name='stein102' timestamp='1307409172' post='4820323']
[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 />[/quote]<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

[b][url="https://secure.wikimedia.org/wikipedia/en/wiki/Recursive_descent_parser"]Recursive descent parser[/url][/b]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this