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

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Dernier (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

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