Slides for "Better up front: Generating parsers in ANSI C" given at Software for the Public Domain (FreeSoft) Workshop, Madrid 1995. A preprint of the full paper is available at http://www.academia.edu/2493896/Better_up_front_Generating_parsers_in_ANSI_C .
Generative AI for Technical Writer or Information Developers
Better up front: Generating parsers in ANSI C (FreeSoft '95)
1. ??
BETTER UP FRONT
Generating Parsers in ANSI C
Peter T. Breuer
Depto. de Ingenier´ıa de Sistemas Telem´aticos
Universidad Polit´ecnica de Madrid
Univ. Carlos III, Madrid Free Software Workshop – 14-15 September 1995 1
2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
8. 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
9. 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 OKn",$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
10. 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
11. 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
12. 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
13. MIDDLE Example - palindromes: Dirty Detail ??
@ both(p,q) = tmp {@ &tmp @}ptmp
@ ] px {@ *$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
14. 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
15. 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