SlideShare a Scribd company logo
1 of 33
Download to read offline
Parsing
                           for Fun and Profit
                            (but mainly fun)


                                                  Ash Moran
                                   ash.moran@patchspace.co.uk
                                               PatchSpace Ltd
Saturday, 23 February 13
What?


Saturday, 23 February 13
Parsing


                           Adding structure and meaning to text




Saturday, 23 February 13
Parsing Human
                                  Languages
                           Jake stretched his legs
                           “Jake”, “stretched”, “his”, “legs”
                           “Jake”<noun>, “stretched”<verb, past>,
                           “his”<possessive pronoun>, “legs”<noun>
                           “Jake” <noun, subject>, “stretched”, (“his”,
                           “legs”)<noun phrase, object>


Saturday, 23 February 13
Parsing Computer
                                Languages
                           “foo = bar + 123”
                           “foo”, “=”, “bar”, “+”, “123”
                           “foo”<var>, “=”<assignment_op>,
                           “bar”<var>, “+”<op_plus>,
                           “123”<int_literal>



Saturday, 23 February 13
Why?


Saturday, 23 February 13
Not just
           compiling!
            Compilers breathe fire.




Saturday, 23 February 13
Pretty Printing
Saturday, 23 February 13
Pretty
                Printing
                           gofmt

                 http://gofmt.com/




Saturday, 23 February 13
Code Smell Detectors
                           https://rubygems.org/gems/reek

Saturday, 23 February 13
Code Smell Detectors
Saturday, 23 February 13
Other ideas
                           Code metrics
                           Bug detectors
                           Domain-specific languages
                           Language translators (e.g. Ruby -> PHP)
                           Code obfuscators
                           Alternative syntaxes (e.g. CoffeeScript)
                           Refactoring tools
Saturday, 23 February 13
How?


Saturday, 23 February 13
Step 1
          3 year computer science
                   degree




Saturday, 23 February 13
Lexing/Tokenising


                   if x > 100 then return “big” else return “small”
                   if x > 100 then return “big” else return “small”




Saturday, 23 February 13
Tree Building
                   if x > 100 then return “big” else return a + b
                                           if

                               >         then     else

                           x       100   return     return

                                         “big”           +
                                                   a         b


Saturday, 23 February 13
Parsing Expression
                                Grammars

                           Like regular expressions, but can handle
                           recursion, e.g. HTML
                           Not actually that much harder to use




Saturday, 23 February 13
Regexes and HTML




Saturday, 23 February 13
Treetop PEG grammar
Saturday, 23 February 13
Doing Sums


Saturday, 23 February 13
Switch to
                           Sublime Text, idiot
                               Code is now available:
         https://github.com/patchspace/parsing_for_fun_and_profit/




Saturday, 23 February 13
A Ruby Syntax
                             Highlighter


Saturday, 23 February 13
What


                           A tool to read in simple Ruby source and
                           output syntax highlighted HTML




Saturday, 23 February 13
Why

                           Because I thought it would be fun
                           It was
                           Because I thought it would be easy
                           …



Saturday, 23 February 13
Why



Saturday, 23 February 13
How
                           Build a parse tree of the Ruby source
                           Walk the tree and spit out a <span>
                           element for each bit of text
                           Oh yes, make sure each line goes in <div>
                           and <pre> tags
                           Wrap it in <html>
                           And for bonus points, do some fancy
                           method highlighting
Saturday, 23 February 13
Switch to Chrome,
                                 idiot


Saturday, 23 February 13
Switch to
        Sublime Text again, idiot
                            Code is now available:
           https://github.com/patchspace/parsing_for_fun_and_profit/




Saturday, 23 February 13
We’re doing this the
                                hard way

                           Ruby’s grammar is too complex and
                           undefined to easily implement as a PEG
                           Tools for parsing Ruby already exist




Saturday, 23 February 13
Ripper (Ruby 1.9.3)
Saturday, 23 February 13
Learn
                           more!
        Skip theoretical physics,
       start by playing with Lego




Saturday, 23 February 13
Do more
                           Ideas you might like to try:
                            CSV parser
                            JSON parser (return arrays & hashes)
                            XML parser
                            JSON highlighter
                            A simple JavaScript minifier (just kill
                            whitespace)

Saturday, 23 February 13
Thank you

                                              Ash Moran
                               ash.moran@patchspace.co.uk
                                           PatchSpace Ltd
Saturday, 23 February 13

More Related Content

More from PatchSpace Ltd

Conflict in Complex Systems
Conflict in Complex SystemsConflict in Complex Systems
Conflict in Complex SystemsPatchSpace Ltd
 
Personal Kanban (lightning talk)
Personal Kanban (lightning talk)Personal Kanban (lightning talk)
Personal Kanban (lightning talk)PatchSpace Ltd
 
Why Won't My Car Start?
Why Won't My Car Start?Why Won't My Car Start?
Why Won't My Car Start?PatchSpace Ltd
 
ShRUG 5 - Scottish Ruby Conf edition
ShRUG 5  - Scottish Ruby Conf editionShRUG 5  - Scottish Ruby Conf edition
ShRUG 5 - Scottish Ruby Conf editionPatchSpace Ltd
 
Encouraging Agile Discipline
Encouraging Agile DisciplineEncouraging Agile Discipline
Encouraging Agile DisciplinePatchSpace Ltd
 
From Specification To Success
From Specification To SuccessFrom Specification To Success
From Specification To SuccessPatchSpace Ltd
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsPatchSpace Ltd
 
NWRUG July 2009 - Darcs
NWRUG July 2009 - DarcsNWRUG July 2009 - Darcs
NWRUG July 2009 - DarcsPatchSpace Ltd
 
Elephants In The Meeting Room
Elephants In The Meeting RoomElephants In The Meeting Room
Elephants In The Meeting RoomPatchSpace Ltd
 

More from PatchSpace Ltd (9)

Conflict in Complex Systems
Conflict in Complex SystemsConflict in Complex Systems
Conflict in Complex Systems
 
Personal Kanban (lightning talk)
Personal Kanban (lightning talk)Personal Kanban (lightning talk)
Personal Kanban (lightning talk)
 
Why Won't My Car Start?
Why Won't My Car Start?Why Won't My Car Start?
Why Won't My Car Start?
 
ShRUG 5 - Scottish Ruby Conf edition
ShRUG 5  - Scottish Ruby Conf editionShRUG 5  - Scottish Ruby Conf edition
ShRUG 5 - Scottish Ruby Conf edition
 
Encouraging Agile Discipline
Encouraging Agile DisciplineEncouraging Agile Discipline
Encouraging Agile Discipline
 
From Specification To Success
From Specification To SuccessFrom Specification To Success
From Specification To Success
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
 
NWRUG July 2009 - Darcs
NWRUG July 2009 - DarcsNWRUG July 2009 - Darcs
NWRUG July 2009 - Darcs
 
Elephants In The Meeting Room
Elephants In The Meeting RoomElephants In The Meeting Room
Elephants In The Meeting Room
 

Recently uploaded

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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 Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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
 

Recently uploaded (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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 Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 

Parsing for Fun and Profit

  • 1. Parsing for Fun and Profit (but mainly fun) Ash Moran ash.moran@patchspace.co.uk PatchSpace Ltd Saturday, 23 February 13
  • 3. Parsing Adding structure and meaning to text Saturday, 23 February 13
  • 4. Parsing Human Languages Jake stretched his legs “Jake”, “stretched”, “his”, “legs” “Jake”<noun>, “stretched”<verb, past>, “his”<possessive pronoun>, “legs”<noun> “Jake” <noun, subject>, “stretched”, (“his”, “legs”)<noun phrase, object> Saturday, 23 February 13
  • 5. Parsing Computer Languages “foo = bar + 123” “foo”, “=”, “bar”, “+”, “123” “foo”<var>, “=”<assignment_op>, “bar”<var>, “+”<op_plus>, “123”<int_literal> Saturday, 23 February 13
  • 7. Not just compiling! Compilers breathe fire. Saturday, 23 February 13
  • 9. Pretty Printing gofmt http://gofmt.com/ Saturday, 23 February 13
  • 10. Code Smell Detectors https://rubygems.org/gems/reek Saturday, 23 February 13
  • 12. Other ideas Code metrics Bug detectors Domain-specific languages Language translators (e.g. Ruby -> PHP) Code obfuscators Alternative syntaxes (e.g. CoffeeScript) Refactoring tools Saturday, 23 February 13
  • 14. Step 1 3 year computer science degree Saturday, 23 February 13
  • 15. Lexing/Tokenising if x > 100 then return “big” else return “small” if x > 100 then return “big” else return “small” Saturday, 23 February 13
  • 16. Tree Building if x > 100 then return “big” else return a + b if > then else x 100 return return “big” + a b Saturday, 23 February 13
  • 17. Parsing Expression Grammars Like regular expressions, but can handle recursion, e.g. HTML Not actually that much harder to use Saturday, 23 February 13
  • 18. Regexes and HTML Saturday, 23 February 13
  • 20. Doing Sums Saturday, 23 February 13
  • 21. Switch to Sublime Text, idiot Code is now available: https://github.com/patchspace/parsing_for_fun_and_profit/ Saturday, 23 February 13
  • 22. A Ruby Syntax Highlighter Saturday, 23 February 13
  • 23. What A tool to read in simple Ruby source and output syntax highlighted HTML Saturday, 23 February 13
  • 24. Why Because I thought it would be fun It was Because I thought it would be easy … Saturday, 23 February 13
  • 26. How Build a parse tree of the Ruby source Walk the tree and spit out a <span> element for each bit of text Oh yes, make sure each line goes in <div> and <pre> tags Wrap it in <html> And for bonus points, do some fancy method highlighting Saturday, 23 February 13
  • 27. Switch to Chrome, idiot Saturday, 23 February 13
  • 28. Switch to Sublime Text again, idiot Code is now available: https://github.com/patchspace/parsing_for_fun_and_profit/ Saturday, 23 February 13
  • 29. We’re doing this the hard way Ruby’s grammar is too complex and undefined to easily implement as a PEG Tools for parsing Ruby already exist Saturday, 23 February 13
  • 31. Learn more! Skip theoretical physics, start by playing with Lego Saturday, 23 February 13
  • 32. Do more Ideas you might like to try: CSV parser JSON parser (return arrays & hashes) XML parser JSON highlighter A simple JavaScript minifier (just kill whitespace) Saturday, 23 February 13
  • 33. Thank you Ash Moran ash.moran@patchspace.co.uk PatchSpace Ltd Saturday, 23 February 13