473,386 Members | 1,736 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,386 software developers and data experts.

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_block | function_block |
conditional_block | loop_block | code | ....

maybe structures_block is something like

structures_block = structure_block | struct_block | class_block |
template_class_block | ....

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

private_block = ':' (structure_blocks | 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.includes[0] = "iostream";
program.includes[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.functions[0].return_type = "void";
program.functions[0].name = "main";
program.functions[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("main", program.functions);

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

insert_code(program.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 6266
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.supernews.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 transformational 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
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...
193
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...
17
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 ,...
9
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...
7
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...
852
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...
0
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:...
6
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....
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.