SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
??
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

Contenu connexe

En vedette (7)

Yacc
YaccYacc
Yacc
 
Lex
LexLex
Lex
 
Programming in Ansi C
Programming in Ansi CProgramming in Ansi C
Programming in Ansi C
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
Parsing example
Parsing exampleParsing example
Parsing example
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 

Plus de Peter Breuer

Avoiding Hardware Aliasing
Avoiding Hardware AliasingAvoiding Hardware Aliasing
Avoiding Hardware AliasingPeter Breuer
 
Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)
Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)
Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)Peter Breuer
 
Certifying (RISC) Machine Code Safe from Aliasing (OpenCert 2013)
Certifying (RISC) Machine Code Safe from Aliasing  (OpenCert 2013)Certifying (RISC) Machine Code Safe from Aliasing  (OpenCert 2013)
Certifying (RISC) Machine Code Safe from Aliasing (OpenCert 2013)Peter Breuer
 
Tutorial: Formal Methods for Hardware Verification - Overview and Application...
Tutorial: Formal Methods for Hardware Verification - Overview and Application...Tutorial: Formal Methods for Hardware Verification - Overview and Application...
Tutorial: Formal Methods for Hardware Verification - Overview and Application...Peter Breuer
 
A Semantic Model for VHDL-AMS (CHARME '97)
A Semantic Model for VHDL-AMS (CHARME '97)A Semantic Model for VHDL-AMS (CHARME '97)
A Semantic Model for VHDL-AMS (CHARME '97)Peter Breuer
 
The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)
The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)
The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)Peter Breuer
 
Higher Order Applicative XML (Monterey 2002)
Higher Order Applicative XML (Monterey 2002)Higher Order Applicative XML (Monterey 2002)
Higher Order Applicative XML (Monterey 2002)Peter Breuer
 
Raiding the Noosphere
Raiding the NoosphereRaiding the Noosphere
Raiding the NoospherePeter Breuer
 
Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...
Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...
Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...Peter Breuer
 
Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...
Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...
Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...Peter Breuer
 
Open Source Verification under a Cloud (OpenCert 2010)
Open Source Verification under a Cloud (OpenCert 2010)Open Source Verification under a Cloud (OpenCert 2010)
Open Source Verification under a Cloud (OpenCert 2010)Peter Breuer
 

Plus de Peter Breuer (11)

Avoiding Hardware Aliasing
Avoiding Hardware AliasingAvoiding Hardware Aliasing
Avoiding Hardware Aliasing
 
Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)
Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)
Empirical Patterns in Google Scholar Citation Counts (CyberPatterns 2014)
 
Certifying (RISC) Machine Code Safe from Aliasing (OpenCert 2013)
Certifying (RISC) Machine Code Safe from Aliasing  (OpenCert 2013)Certifying (RISC) Machine Code Safe from Aliasing  (OpenCert 2013)
Certifying (RISC) Machine Code Safe from Aliasing (OpenCert 2013)
 
Tutorial: Formal Methods for Hardware Verification - Overview and Application...
Tutorial: Formal Methods for Hardware Verification - Overview and Application...Tutorial: Formal Methods for Hardware Verification - Overview and Application...
Tutorial: Formal Methods for Hardware Verification - Overview and Application...
 
A Semantic Model for VHDL-AMS (CHARME '97)
A Semantic Model for VHDL-AMS (CHARME '97)A Semantic Model for VHDL-AMS (CHARME '97)
A Semantic Model for VHDL-AMS (CHARME '97)
 
The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)
The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)
The mixed-signal modelling language VHDL-AMS and its semantics (ICNACSA 1999)
 
Higher Order Applicative XML (Monterey 2002)
Higher Order Applicative XML (Monterey 2002)Higher Order Applicative XML (Monterey 2002)
Higher Order Applicative XML (Monterey 2002)
 
Raiding the Noosphere
Raiding the NoosphereRaiding the Noosphere
Raiding the Noosphere
 
Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...
Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...
Abstract Interpretation meets model checking near the 1000000 LOC mark: Findi...
 
Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...
Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...
Detecting Deadlock, Double-Free and Other Abuses in a Million Lines of Linux ...
 
Open Source Verification under a Cloud (OpenCert 2010)
Open Source Verification under a Cloud (OpenCert 2010)Open Source Verification under a Cloud (OpenCert 2010)
Open Source Verification under a Cloud (OpenCert 2010)
 

Dernier

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 

Dernier (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
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