Re: Goldie Parsing System v0.4 Released - Now for D2

2011-04-15 Thread Daniel Gibson
Am 15.04.2011 09:50, schrieb Nick Sabalausky:
 Since you're talking about C, you'll probably want to run your original C 
 code through the preprocess-only option of a real C compiler. (I *think* 
 DMC will do that.) Then parse the resulting preprocessed C files with 
 Goldie. (Although if your goal is an HTOD-like tool, maybe you would need to 
 deal with the original un-preprocessed source directly. 

Why? Just call the preprocessor from your tool or from a wrapping script
and go on with the preprocessed C code. Should be much easier and more
compatible because C compilers ought to know how to preprocess correctly.
For GCC the option you're looking for is -E, btw.

 If Golde's grammar 
 langauge doesn't seem quite up to the task, it probably wouldn't bee too 
 hard to just manually make a basic C preprocessor.)
 

Cheers,
- Daniel


Re: Goldie Parsing System v0.4 Released - Now for D2

2011-04-15 Thread Nick Sabalausky
Daniel Gibson metalcae...@gmail.com wrote in message 
news:io8u12$132q$1...@digitalmars.com...
 Am 15.04.2011 09:50, schrieb Nick Sabalausky:
 Since you're talking about C, you'll probably want to run your original C
 code through the preprocess-only option of a real C compiler. (I 
 *think*
 DMC will do that.) Then parse the resulting preprocessed C files with
 Goldie. (Although if your goal is an HTOD-like tool, maybe you would need 
 to
 deal with the original un-preprocessed source directly.

 Why? Just call the preprocessor from your tool or from a wrapping script
 and go on with the preprocessed C code. Should be much easier and more
 compatible because C compilers ought to know how to preprocess correctly.
 For GCC the option you're looking for is -E, btw.


If by your tool mean a program that uses Goldie to process C code, then 
yea, that's what I meant.

If you meant that Goldie should invoke a C preprocessor directly, that's a 
bit tricky: Goldie is a generalized parsing tool (sort of like ANTLR or 
Spirit), so it doesn't really know Ok, this is supposed to be C. It just 
parses according to whatever grammar it's given. Of course, it's not 
entirely out of the question to have some sort of system for specifying that 
a source should have XYZ tool (such as C preprocessor) invoked on it 
first, etc, but it's probably easiest if programs using Goldie just invoke 
whatever other tools they need by themselves.

(Sorry if I've stil misunderstood - it's late over here ;) ) 




Re: Goldie Parsing System v0.4 Released - Now for D2

2011-04-15 Thread Daniel Gibson
Am 15.04.2011 10:13, schrieb Nick Sabalausky:
 Daniel Gibson metalcae...@gmail.com wrote in message 
 news:io8u12$132q$1...@digitalmars.com...
 Am 15.04.2011 09:50, schrieb Nick Sabalausky:
 Since you're talking about C, you'll probably want to run your original C
 code through the preprocess-only option of a real C compiler. (I 
 *think*
 DMC will do that.) Then parse the resulting preprocessed C files with
 Goldie. (Although if your goal is an HTOD-like tool, maybe you would need 
 to
 deal with the original un-preprocessed source directly.

 Why? Just call the preprocessor from your tool or from a wrapping script
 and go on with the preprocessed C code. Should be much easier and more
 compatible because C compilers ought to know how to preprocess correctly.
 For GCC the option you're looking for is -E, btw.

 
 If by your tool mean a program that uses Goldie to process C code, then 
 yea, that's what I meant.
 

I meant Andrej's hypothetical tool using Goldie to process C code :-)


Re: Goldie Parsing System v0.4 Released - Now for D2

2011-04-15 Thread Andrej Mitrovic
I've used your tool yesterday. I used it on a simple C file with the
ANSI C grammar from the gold website. It does seem to work fine, but
yeah I have to preprocess a C file first (I've spent so much time with
D that I almost completely forgot about the C preprocessor in the
first place).

I've tried a file with your ParseAnything sample. It works ok as long
as all the types are defined. If not I usually get a Token exception
of some sort. Is this considered the semantic pass stage?

Btw, is there a grammar file for C99? What about C++, I haven't seen a
grammar on the Gold website? (well, C++ is a monster, I know..).

I'm also trying to figure out whether to go with the static or dynamic
approach (I've looked at your docs). The static examples seem quite
complex, but perhaps they're more reliable. I think I'll do a few
tryouts with dynamic style since it looks much easier to do. If I get
anything done you'll know about it. :)


Re: Goldie Parsing System v0.4 Released - Now for D2

2011-04-15 Thread Nick Sabalausky
Andrej Mitrovic andrej.mitrov...@gmail.com wrote in message 
news:mailman.3531.1302884207.4748.digitalmars-d-annou...@puremagic.com...
 I've used your tool yesterday. I used it on a simple C file with the
 ANSI C grammar from the gold website. It does seem to work fine, but
 yeah I have to preprocess a C file first (I've spent so much time with
 D that I almost completely forgot about the C preprocessor in the
 first place).

 I've tried a file with your ParseAnything sample. It works ok as long
 as all the types are defined. If not I usually get a Token exception
 of some sort. Is this considered the semantic pass stage?


Like any generalized parsing tool (AFAIK), Goldie doesn't really have a 
semantic stage (because language semantics isn't something that's easily 
formalized).

Probably the C grammar just considers something in your source to be either 
a syntax or grammatical error. (This could be a bug or limitation in the C 
grammar.) Goldie currently handles syntax/grammatical errors by throwing a 
ParseException when it detects all the errors it can find. The message of 
the exception is the filename(line:col): Error: Description of error 
message that you'd normally expect a compiler to output. Most of the apps in 
Goldie catch this exception and just output the message, but I guess I 
didn't do that in ParseAnything.

Of course, it could also be a bug in either ParseAnything or Goldie. Can you 
send one of the C files that's getting an error? I'll take a look and see 
what's going on.

You may want to try goldie-parse instead of goldie-parseAnything (I 
really should rename one of them, it's probably confusing). 
goldie-parseAnything is mainly intended as an example of how to use Goldie 
(like the Calculator examples). goldie-parse is the one that outputs JSON.


 Btw, is there a grammar file for C99? What about C++, I haven't seen a
 grammar on the Gold website? (well, C++ is a monster, I know..).


Not that I'm aware of. But if you know the differences between ANSI C and 
C99 you should be able to modify the ANSI C grammar and turn it into a C99. 
The grammar description language should be very easy to understand if you're 
familiar with BNF and regex (In fact, the grammar definition langauge 
doesn't even use the barely-readable Perl regex syntax - it uses a far more 
readable equivalent instead). BTW, Tip on the grammar language: Everything 
enclosed in angle brackets is a nonterminal.

And yea, C++ is a beast. And one of C++'s biggest issues is that, not only 
does it have the preprocessor, but what's worse: the parsing is dependent on 
the semantics pass. I'd say that any generalized parsing tool that can do 
C++ properly is doing an *incredibly* damn good job.


 I'm also trying to figure out whether to go with the static or dynamic
 approach (I've looked at your docs). The static examples seem quite
 complex, but perhaps they're more reliable. I think I'll do a few
 tryouts with dynamic style since it looks much easier to do.

The general recommendation is to use static whenever you just have one 
specific grammar you're trying to deal with (because it provides better 
protection against mistakes). But you're right, the dynamic style may be an 
easier way to learn Goldie.

If you haven't already, you may wat to look at the source for the calculator 
examples. They're both the exact same program, but one does it the static 
way, and the other does it the dynamic way.

 If I get anything done you'll know about it. :)

Cool, appreciated :)




Re: Goldie Parsing System v0.4 Released - Now for D2

2011-04-15 Thread Nick Sabalausky
Andrej Mitrovic Wrote:

 What I meant was that code like this will throw if MyType isn't
 defined anywhere:
 
 int main(int x)
 {
 MyType var;
 }
 
 goldie.exception.UnexpectedTokenException@src\goldie\exception.d(35):
 test.c(3:12): Unexpected Id: 'var'
 
 It looks like valid C /syntax/, except that MyType isn't defined. But
 this will work:
 struct MyType {
int field;
 };
 int main(int x)
 {
 struct MyType var;
 }
 
 So either Goldie or ParseAnything needs to have all types defined.
 Maybe this is obvious, but I wouldn't know since I've never used a
 parser before. :p
 
 Oddly enough, this one will throw:
 typedef struct {
 int field;
 } MyType;
 int main(int x)
 {
 MyType var;
 }
 
 goldie.exception.UnexpectedTokenException@src\goldie\exception.d(35):
 test.c(7:12): Unexpected Id: 'var'
 
 This one will throw as well:
 struct SomeStruct {
 int field;
 };
 typedef struct SomeStruct MyType;
 int main(int x)
 {
 MyType var;
 }
 
 goldie.exception.UnexpectedTokenException@src\goldie\exception.d(35):
 test.c(13:12): Unexpected Id: 'myvar'
 
 Isn't typedef a part of ANSI C?

I'm not at my computer right now, so I can't check, but it sounds like the 
grammar follows the really old C-style of requiring structs to be declared with 
struct StructName varName. Apperently it doesn't take into account the 
possibility of typedefs being used to eliminate that. When I get home, I'll 
check, I think it may be an easy change to the grammar.