473,545 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grammars for C++

Anyone know where I can get a (E)BNF like grammar for C++ to possibly use
either for analysis or for yacc/bison(++)?

On a side note I was thinking about writing my own lexer and syntaxer and
was thinking about ways to approach it. The way I see it is that most HLL
languages use nesting structures and if I could somehow define a grammar
that brings out this structure then I could use it for an application that I
need which is basicaly a source to source translator for C++(but I'd like to
design the parser and lexer to work for any grammar(sorta like bison).

Anyways, what I was thinking is that every block in say C++ is either a
block that is part of some structure defining or part of some code execution
grouping.

Maybe a grammer for it would be something like

program = block

block = block | header_block | structures_bloc k | function_block |
conditional_blo ck | loop_block | code | ....

maybe structures_bloc k is something like

structures_bloc k = structure_block | struct_block | class_block |
template_class_ block | ....

class_block = 'class' name [: (base_classes,)] '{' structures_bloc ks |
private_block | public_block | protected_block '}'

private_block = ':' (structure_bloc ks | code)

......

So the grammar is kinda defined in a hierarchy of blocks. (Note that I'm
just starting to learn grammars and so the stuff above isn't going to be
perfect or make complete sense... its just for an idea I'm trying to get
across... and I know one can get some stupid things from it such as a
conditional block that isn't in some type of code block.. but just an
example(off the top of my head)).
Now, it may turn out that it doesn't matter how one writes the grammar(if
they represent the same language) that the parsing will always return the
same "parse tree"(which is what I'm kinda thinking but I'm not sure).

The reason I kinda need the "block" idea is that I want to reference the
code structure sorta like as an object.

lets say I have the program code as follows:

#include <iostream>
#include <string>

class test
{
private:
int i;
public:
test()
{
i = 0;
}
}

void main()
{
return 0;
}

then I want to represent that code sorta like an "object":

program.include s[0] = "iostream";
program.include s[1] = "string";
program.classes[0].name = "test";
program.classes[0].private.data[0].type = "int";
program.classes[0].private.data[0].name = "i";
program.classes[0].constructor[0].arg[0] = "";
program.classes[0].constructor[0].code.statement[0] = "i = 0";
program.functio ns[0].return_type = "void";
program.functio ns[0].name = "main";
program.functio ns[0].code.statement[0] = "return 0;";
etc....

or something like that.

so say I wanted to add some code to the "main" function I could then do
somethign like

int i = find_block("mai n", program.functio ns);

then program.functio ns[i].name = "main"; and I could do something like

insert_code(pro gram.functions[i].code, "test t;", 0);

and that would insert the test t; line as

void main()
{
test t;
return 0;
}
Anyone have any ideas about doing this? Can I simply use flex and bison to
parse the original C++ code and easily "formulate" it into the "object" like
referencing of the different blocks?

Thanks,
Jon
Jul 23 '05 #1
6 6286
You can find the complete grammer of C++ in book, The C++ Programming
Language by Bjarne Stroustrup (Addison Wesley). Its given in the
Appendix A of the book.

Jul 23 '05 #2
You sure that's a COMPLETE gramma?
Jul 23 '05 #3
The auther says about the grammer in the introducton:

This summary of C++syntax is intended to be an aid to comprehension. It
is not an exact statement of the language. In particular, the grammar
described here accepts a superset of valid C++constructs.
Disambiguation rules (§A.5, §A.7) must be applied to distinguish
expressions from declarations. Moreover, access control, ambiguity, and
type rules must be used to weed out syntactically valid but meaningless
constructs.

Jul 23 '05 #4
Jon Slaughter wrote:
Anyone know where I can get a (E)BNF like grammar for C++ to possibly use
either for analysis or for yacc/bison(++)?


This was linked a week or two ago by Steven T. Hatton. Hyperlinked BNF
Grammar:
http://www.nongnu.org/hcb/

Jul 23 '05 #5
"Jon Slaughter" <Jo***********@ Hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.com...
Anyone know where I can get a (E)BNF like grammar for C++ to possibly use
either for analysis or for yacc/bison(++)?


Most people that try this think the issue is the grammar.
If you get past the "hills" of troubles with getting
a trustworthy grammar, let alone an LALR(1)
grammar, you'll discover
the semantics of C++ on the other side are rather
like the Himalayas in comparison: ambiguous
rules, preprocessor, ambiguous
include files, name/type resolution, templates.
If you succeed there, you then get to think
about building machinery to actually carry
out analyses of interest, make changes to
the code, and then regenerate it all without
making any mistakes. Finally, you get to fight
with the fact that C++ comes in a bunch of dialects...

We've spent the better part of an elapsed decade
and/or a man-century depending on your perspective,
building transformationa l machinery to carry out general
parsing/analysis/transformation, and a signficant
chunk of the last 5 years building robust C++
parser front ends for that machinery, using
extremely experienced computer science language
experts.

I don't want to rain on your parade,
but kids, please don't try this at home,
unless your goals are extremely limited.

See http://www.semdesigns.com/Products/F...pFrontEnd.html
--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc. www.semdesigns.com
Jul 24 '05 #6
Jon Slaughter schrieb:
Anyone know where I can get a (E)BNF like grammar for C++ to possibly use
either for analysis or for yacc/bison(++)?

On a side note I was thinking about writing my own lexer and syntaxer and
was thinking about ways to approach it. The way I see it is that most HLL
languages use nesting structures and if I could somehow define a grammar
that brings out this structure then I could use it for an application that I
need which is basicaly a source to source translator for C++(but I'd like to
design the parser and lexer to work for any grammar(sorta like bison).


Please look at the following information sources and tools.
1. Edward D. Willink: Meta-Compilation for C++ - transformation and
filtering of a superset to the target language
http://www.computing.surrey.ac.uk/research/dsrg/fog/
http://citeseer.ist.psu.edu/251920.html

2. James F. Power, Tanton H. Gibbs and Brian A. Malloy: Keystone -
token decoration
http://keystone.sourceforge.net/research.shtml

3. Article "Parsing C++"
http://www.nobugs.org/developer/parsingcpp/

4. http://en.wikipedia.org/wiki/OpenC_Plus_Plus

5. http://synopsis.fresco.org/

6. http://doxygen.org/

Regards,
Markus

Jul 24 '05 #7

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

Similar topics

9
3509
by: Tran Tuan Anh | last post by:
Hi all, I am searching for a suitable parsing tool to parse C++ for a research project. Actually I am parsing SystemC, it is a C++ lib with some macros, hence I need to modify the grammar a little bit to handle some new keywords. I need to construct the Control Flow Graph and play around with it a bit. So far, I have found SPARK and PLY...
193
9425
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able...
17
3473
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means...
9
1196
by: seberino | last post by:
Is there any advantage to a language having a nice mathematically compact grammar like LISP does? (or at least used to?) Many have admired the mathematically simple grammar of LISP in which much of the language is built up from conses IIRC. Python's grammar seems complicated by comparison. Is this anything to worry about?
7
5674
by: (Jamie Andrews) | last post by:
For a research project, we're looking for a reliable parser for C that will take an ANSI C program and yield a tree representation of the program (as a Java or C++ object). Of course a grammar e.g. in jflex/jbison that will yield the same thing is fine too. We have been able to find some grammars and parsers, of unknown reliability, that...
852
27958
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for my general education. Mark
0
3884
by: JosAH | last post by:
Greetings, this week we discuss the design of the syntactic aspects of our little language; it helps with the design for the parser(s) that recognize such syntax. Last week we saw the tokenizer: it just groups sequences of bytes into tokens; it doesn't know anything about any syntax of any language at all. That's the job of the parser; a...
6
4403
by: aparna881 | last post by:
Hello every one.. I am planning to make a lexcial analyzer in C++, the keywords, opertators etc for the language that the lexer would take lexmes from are all given to us as a set of requirements. I wanted to know how do i go about making this.. Thanks Ap
1
1573
by: Christian Welzel | last post by:
Hi there, i currently evaluating db2 express-c and therefor i wanted to migrate my current database from mysql5 to the express-c... i didn't get it working with mysqldump, and finally found the ibm data migration toolkit. But whenever i want to start the conversation i get this exception: Sun Mar 23 23:07:28 CET 2008 ERROR MySQL...
0
7496
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7941
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.