Grammars and Parsers
-
Dear experts, may be I have a stupid question for the experts. During my complete sw-career, if I had to parse some information from a text-file, I wrote some hand-crafted functions/classes which were full of (stringcompare, if-then-else-constructs, nested "for"-loops, ....). Finally this code was always totally unmaintainable and unreusable. An example: Getting the include-structure of a c++ - based program can easily get complicated, because the active include-files get constrained on nested defines. Now I read something on grammars and parsers which are used for compiler-construction. This seems to be very interesting to a lot of practical problems (not only to my include-structure-parsing-problem). Due to my beginners level of knowhow I have some questions: 1. Which links/books are available for beginners? 2. Is there a way to automatically create a parser solely from a grammar? 3. Is there any C# - Example available anywhere (e.b. at Codeproject) Kind Regards
-
Dear experts, may be I have a stupid question for the experts. During my complete sw-career, if I had to parse some information from a text-file, I wrote some hand-crafted functions/classes which were full of (stringcompare, if-then-else-constructs, nested "for"-loops, ....). Finally this code was always totally unmaintainable and unreusable. An example: Getting the include-structure of a c++ - based program can easily get complicated, because the active include-files get constrained on nested defines. Now I read something on grammars and parsers which are used for compiler-construction. This seems to be very interesting to a lot of practical problems (not only to my include-structure-parsing-problem). Due to my beginners level of knowhow I have some questions: 1. Which links/books are available for beginners? 2. Is there a way to automatically create a parser solely from a grammar? 3. Is there any C# - Example available anywhere (e.b. at Codeproject) Kind Regards
Tomerland wrote:
1. Which links/books are available for beginners?
I did a module on compilers at uni, and the text book[^] was quite useful. It's the only book I've ever read on the subject though, so I don't know how it compares. It has several chapters on grammars, parsers and syntax trees.
Tomerland wrote:
2. Is there a way to automatically create a parser solely from a grammar?
The classic tool is Yacc[^]. It generates parsers (in C) from grammars. I'm sure if you look around there will be ports for whatever languages you use. Check out here for some other compiler related tools. http://dinosaur.compilertools.net/[^] http://catalog.compilertools.net/lexparse.html[^] There's Irony[^] for .net. It generates compilers for domain specific languages, when given a grammar. (There's also a CP article on it's use here[^]). I haven't tried this out yet, but it's been on my list of interesting things to look at for at least 6 months :laugh: There's also an article on a tiny parser generator[^] app posted only a few days ago.
Simon
-
Tomerland wrote:
1. Which links/books are available for beginners?
I did a module on compilers at uni, and the text book[^] was quite useful. It's the only book I've ever read on the subject though, so I don't know how it compares. It has several chapters on grammars, parsers and syntax trees.
Tomerland wrote:
2. Is there a way to automatically create a parser solely from a grammar?
The classic tool is Yacc[^]. It generates parsers (in C) from grammars. I'm sure if you look around there will be ports for whatever languages you use. Check out here for some other compiler related tools. http://dinosaur.compilertools.net/[^] http://catalog.compilertools.net/lexparse.html[^] There's Irony[^] for .net. It generates compilers for domain specific languages, when given a grammar. (There's also a CP article on it's use here[^]). I haven't tried this out yet, but it's been on my list of interesting things to look at for at least 6 months :laugh: There's also an article on a tiny parser generator[^] app posted only a few days ago.
Simon
-
Dear experts, may be I have a stupid question for the experts. During my complete sw-career, if I had to parse some information from a text-file, I wrote some hand-crafted functions/classes which were full of (stringcompare, if-then-else-constructs, nested "for"-loops, ....). Finally this code was always totally unmaintainable and unreusable. An example: Getting the include-structure of a c++ - based program can easily get complicated, because the active include-files get constrained on nested defines. Now I read something on grammars and parsers which are used for compiler-construction. This seems to be very interesting to a lot of practical problems (not only to my include-structure-parsing-problem). Due to my beginners level of knowhow I have some questions: 1. Which links/books are available for beginners? 2. Is there a way to automatically create a parser solely from a grammar? 3. Is there any C# - Example available anywhere (e.b. at Codeproject) Kind Regards
-
You might like to look at COCO/R (type into Google and will be the first hit) for generating from grammars. Its my tool of choice because the syntax is easy to learn and its ported to lots of languages (i.e. it can generate C#, Java......) Chris