473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ language grammar?

Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?

The reason for this question is that I was trying to figure out what
the output of the following program be:

int main()
{
int a = 10;
printf("%d %d %d\n",++a,a++,a);
return 0;
}

and I am getting two different results on two different compilers. Can
one definitely answer based on how the laguage is parsed (lef to right
or right to left) to tell the output of the above program?

Thanks,
Divick

Jul 20 '06 #1
6 5550
Divick wrote:
Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?

The reason for this question is that I was trying to figure out what
the output of the following program be:

int main()
{
int a = 10;
printf("%d %d %d\n",++a,a++,a);
return 0;
}
Please see:
http://c-faq.com/expr/evalorder2.html
http://www.parashift.com/c++-faq-lit...html#faq-39.15
http://www.parashift.com/c++-faq-lit...html#faq-39.16
http://www.angelikalanger.com/Articl...ncePoints.html

--
Regards,
Sumit RAJAN <su*********@geoconcept.com>
Jul 20 '06 #2

Divick wrote:
Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?
The C and the C++ grammars are not context-free, so they cannot be
handled by these parsing techniques alone.

The lexical category of tokens depends on their semantics (how they are
declared).

In order to decide how to categorize a lexeme, the lexical analyzer
must sometimes make use of semantic information gathered up to that
point.
The reason for this question is that I was trying to figure out what
the output of the following program be:
int main()
{
int a = 10;
printf("%d %d %d\n",++a,a++,a);
return 0;
}

and I am getting two different results on two different compilers.
This is because of semantics, not grammar. In C, the grammar for a
function call is:

postfix-expression:
...
postfix-expression ( argument-expression-list_opt )
...

argument-expression-list:
assignment-expression
argument-expression-list , assignment-expression

As you can see, this is left recursion which generates a nice, linear
parse tree sloping to the left.

The shape of that tree has precisely nothing to do with the evaluation
order of those arguments, which is unspecified.
one definitely answer based on how the laguage is parsed (lef to right
or right to left) to tell the output of the above program?
What does /parsing/ have to do with /evaluation/ semantics?

You are overlooking the basic fact that control flow can jump around
arbitrarily within a program, in spite of that program being produced
from source code that is read from top to bottom.

If parsing determined evaluation order, then programs would execute
from top to bottom, and wouldn't have any loops or function calls.

Jul 20 '06 #4
Divick wrote:
Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?

The reason for this question is that I was trying to figure out what
the output of the following program be:

int main()
{
int a = 10;
printf("%d %d %d\n",++a,a++,a);
return 0;
}

and I am getting two different results on two different compilers. Can
one definitely answer based on how the laguage is parsed (lef to right
or right to left) to tell the output of the above program?

What you are doing is undefined behavior. You have modified the same
variable twice without an intervening sequence point.


Brian
Jul 20 '06 #5
Divick wrote:
Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?

The reason for this question is that I was trying to figure out what
the output of the following program be:

int main()
{
int a = 10;
printf("%d %d %d\n",++a,a++,a);
return 0;
}

and I am getting two different results on two different compilers. Can
one definitely answer based on how the laguage is parsed (lef to right
or right to left) to tell the output of the above program?

Thanks,
Divick
AFAIK the C++ grammar is not LR(k) for any k and that would certainly
eliminate LALR or LL as possible parsing engines that could
unambiguously parse the entire C++ grammar. To properly define the
grammar I think you would need something like Wingarten grammars as used
to define the Algol68 language but you really don't want to go there
unless you really want your head to hurt - even the C++ Standard doesn't
try that one.
Jul 20 '06 #6

"James Bannon" <ja**********@ntlworld.comwrote in message
news:Ea******************@newsfe5-win.ntli.net...
Divick wrote:
Hi all,
can anyone tell me what kind of parser is used to parse C++
language i.e. LALR or LL (k?) ? Or is it implementation dependent?

and I am getting two different results on two different compilers. Can
one definitely answer based on how the laguage is parsed (lef to right
or right to left) to tell the output of the above program?

Thanks,
Divick
AFAIK the C++ grammar is not LR(k) for any k and that would certainly
eliminate LALR or LL as possible parsing engines that could
unambiguously parse the entire C++ grammar. To properly define the
grammar I think you would need something like Wingarten grammars as used
to define the Algol68 language but you really don't want to go there
unless you really want your head to hurt - even the C++ Standard doesn't
try that one.
The answer is, parsing is implementation dependent.
GNU uses a hand coded parsers, and in fact almost all
the C++ compilers I know about do so, because of
the perceived nasty interactions of types and "legal syntax".

A GLR parser, however, can parse C++ just fine,
from a very clean (well, at least it matches the standard
closely) grammar, although it produces a number of ambiguous parses,
later resolved by knowing types. The end result
is the same after parsing and name/type resolution
is complete: a tree without ambiguities.

We use a GLR parser in our tools.

--
Ira Baxter, CTO
www.semanticdesigns.com
Jul 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew James | last post by:
Gentlemen, I'm currently in the process of designing a language which will be used to specify sets of files on a WebDAV server, encoded in a URL. The aims of the language are to (in no particular...
20
by: Lars Eighner | last post by:
I believe it was here that I recently saw a discussion that pointed out that the language in the public identifier of a DTD referes to the language in which the DTD is written and has no bearing...
8
by: Hermawih | last post by:
Hello , I want your opinion about this . In order to say it clearly , I think I have to describe it in long sentences . I could consider myself as Intermediate/Advance Access Developer ;...
14
by: Magius | last post by:
Hello, I have a question about the correctness of the language grammar for the C# 2.0 specification. I am working with the grammar specified in the June 2005 ECMA-334 standard. Basically, a...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
10
by: Immortalist | last post by:
Various aquisition devices that guide learning along particular pathways towards human biases. And as E.O. Wilson might say mental development appears to be genetically constrained. (1) Language...
13
by: stefaan | last post by:
Hello, I have recently had to deal with an XML-based programming language. (The programs are generated programmatically.) XML leads to a "two-level" parsing problem: first parse the xml into...
43
by: Adem24 | last post by:
The World Joint Programming Language Standardization Committe (WJPLSC) hereby proclaims to the people of the world that a new programming language is needed for the benefit of the whole mankind in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.