Better up front: Generating parsers in ANSI C (FreeSoft '95)
Click here to load reader
-
Upload
peter-breuer -
Category
Technology
-
view
52 -
download
0
description
Transcript of Better up front: Generating parsers in ANSI C (FreeSoft '95)
![Page 1: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/1.jpg)
??
BETTER UP FRONT
Generating Parsers in ANSI C
Peter T. Breuer
Depto. de Ingenierıa de Sistemas Telematicos
Universidad Politecnica de Madrid
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 1
![Page 2: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/2.jpg)
INTRODUCTON ??PRECCX
Class: Compiler Compiler
alias Parser Generator
Friends:
yacc
bison
PCC
yacc++
Automate the production of front-ends by converting a language
specification to a parser/interpreter/compiler.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 2
![Page 3: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/3.jpg)
INTRODUCTON Definition of Terms ??
A parser synthesises an attribute from a parse.
E.g. parse of “1 + 2” might synthesise the value 3.
A parser can inherit an attribute (from an earlier parse).
E.g. If the parser inherits the binding f := (+), then the parse of
“f(1,2)” might be expected to yield 3.
If the parser inherits the binding f := (∗), one might expect it to yield
2.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 3
![Page 4: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/4.jpg)
INTRODUCTION PRECC History ??1988-91 PRECC 1.* no inherited attributes, project REDO, boot-
strap term-rewrite engine/a Brief Editor macro.
1992 May PRECCX 2.01 first quiet public release with inherited at-
tributes as well as synthesised attributes.
1992 Jul PRECCX 2.23 language additions, lex compatibility, internal
improvements, released to MSDOS achive sites.
1993 Aug PRECCX 2.30 forward changes generate incompatibilities,
re-released to archive sites.
1994 Sep PRECCX 2.42 internal “monad model”, integrated treat-
ment of inherited and synthetic attributes. Re-released.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 4
![Page 5: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/5.jpg)
INTRODUCTION PRECC Current ??
1995 PRECCX 2.43
Multi-platform support for compound data as synthetic attributes.
Type-safe.
Minor bug-fixes.
Re-entrant.
Free code, contracted maintenance.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 5
![Page 6: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/6.jpg)
CONTENTS ??1. Introduction
2. Middle
3. Conclusion
@ talk = Introduction
@ Middle
@ Conclusion
I like to recurse
@ Middle = {Introduction Middle Conclusion | Stuff}+
I like to backtrack
@ Stuff = Bit* EVERYBODY HAPPY | Default Stuff
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 6
![Page 7: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/7.jpg)
MIDDLE Pros and cons v. yacc ??
pro: yacc implements a variety of BNF.
pro: yacc compiles to portable C. No support required.
pro: yacc implements well-understood theory. Reliable.
con: yacc BNF is very impoverished. PRECCX’s is full and extensible.
con: yacc is restricted to 1TLA. PRECCX is unbounded.
con: yacc handles ambiguity/context poorly. PRECCX does it well.
con: yacc 1TLA means approximate spex. PRECCX can be exact.
con: yacc output is a monolithic automaton. PRECCX’s is modular.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 7
![Page 8: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/8.jpg)
MIDDLE Licensing ??
Code is free, but copyright.
Contract for maintenance (bug fixes, advice etc.).
About 100 commercial licences issued to corporations, but most don’t
bother (1% of downloads).
Distribution restrictions are that the package must be re-distributed
complete and for free.
Library code and generated code is excluded from distribution restric-
tions.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 8
![Page 9: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/9.jpg)
MIDDLE Example - Fibonacci ??My favorite example - a little parser that only accepts the Fibonacci
sequence 1,1,3,5,8,13,. . . as input.
MAIN(fibber)
@ fibber = { fibs $! }*
@ fibs = fib(1,1)\k@ {: printf("%d terms OK\n",$k); :}
@ fib(a,b) = number(a) <’,’> fib(b,a+b)\k@ {@ k+1 @}
@ | <’.’> <’.’>
@ {: printf("Next terms are %d,%d,..\n",a,b); :}@ {@ 0 @}
@ number(n) = digit(n) | digit(HEAD(n)) number(TAIL(n))
@ digit(n) = <n+’0’>
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 9
![Page 10: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/10.jpg)
MIDDLE Example - Fibonnaci input ??1,1,2,3,5,..
Next terms are 8,13,..
5 terms OK
1,1,2,3,5,8,13,21,34,51,85,..
(line 2 error) failed parse:
probable error at <>1,85,..
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 10
![Page 11: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/11.jpg)
MIDDLE Example - ideal palindromes ??Palindromes are a classic example of a grammar that is difficult/impossible
to define with bounded lookahead.
“dabale arroz a la zorra el abad”
@ pallies = { palindrome $! }*
@ palindrome = ?\x palindrome <$x>
@ | ?
@ | /* empty */
In practice, we will have to parse twice – once to count the letters and
again to see if it is a palindrome (PRECCX does not store enough
branch points to resolve the exact definition above).
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 11
![Page 12: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/12.jpg)
MIDDLE Example - practical palindromes ??I have to use a “macro” that parses the same input twice.
both(p, q) parses once using parser p, synthesizing attribute x, then
backtracks and parses once using parser q(x). q inherits x.
@ palindrome = both(getlen,pal)
@ pal(nc) = )nc==0( /* empty */
@ | )nc==1( ?\x {: printf(”%c”,$x); :}@ | )nc>=2( ?\x {: printf(”%c”,$x); :}@ pal(nc-2) <$x> {: printf(”%c”,$x); :}
@ getlen = line(0)
@ line(nc) = ? line(nc+1) | {@ nc @}
Got that?
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 12
![Page 13: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/13.jpg)
MIDDLE Example - palindromes: Dirty Detail ??@ both(p,q) = \tmp {@ &tmp @}\ptmp@ ] p\x {@ *$ptmp=$x @} [
@ q(*$ptmp)
Don’t ask!
> dabale arroz a la zorra el abad
yow! !woy
> dabalearrozalazorraelabad
dabalearrozalazorraelabad is OK
>
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 13
![Page 14: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/14.jpg)
MIDDLE Applications ??The following is the list of languages known to me to have been
handled via PRECCX. There are many other existing applications.
• Cobol
• Uniform
• Oberon 2
• Occam
• RatFor
• Z
• PRECCX
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 14
![Page 15: Better up front: Generating parsers in ANSI C (FreeSoft '95)](https://reader038.fdocuments.in/reader038/viewer/2022100600/55653e44d8b42ad7648b4c79/html5/thumbnails/15.jpg)
CONCLUSION ??
This is the end. If you are not happy, we will have to backtrack and
start again in another way.
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 15