Grammar and Torture
Written by Mike James   
Thursday, 16 May 2019
Article Index
Grammar and Torture
Extended BNF
Green Dreams
Travelling the Tree

Travelling the Tree

Now that we have said the deeply unfashionable thing that syntax is not isolated from semantics we can now see why we bother to use a grammar analyzer within a compiler.

Put simply a syntax tree or its equivalent can be used to generate the machine code or intermediate code that the expression corresponds to.

The syntax tree can be considered as a program that tells you how to evaluate the expression.

For example, a common method of generating code from a tree is to walk all its nodes using a “depth first” algorithm.

That is, visit the deepest nodes first and generate an instruction corresponding to the value or operator stored at each node. The details of how to do this vary but you can see the general idea in this diagram.

traverse

 

So now you know. We use grammar to parse expressions, to make syntax trees, to generate the code.

Now find out about different types of grammar and parsing methods – they are important.

A Real Grammar Of Arithmetic

You might like to see what a real grammar for simple arithmetic expressions looks like:

<Exp> -> <Exp> + <Term> |<Exp> - <Term> |<Term>
<Term> -> <Term> * <Factor> | <Term> / <Factor>
| <Factor>
<Factor> -> x | y | ... |

Of course it leaves out proper variable names, operators other than  +, -, * and / but it is a reasonable start.

Further reading:

Assemblers and assembly language

The Heart Of A Compiler

John Backus - the Father of Fortran

Peter Naur Dies Aged 87 

Brackets are Trees

Finite state machines

Turing Machines

A Concise Introduction to Languages and Machines

Language Implementation Patterns

The Universe as a Computer

 

A Programmers Guide To Theory
First Draft

There is a more recent version of this:

Now available as a paperback and ebook from Amazon.

A Programmers Guide To Theory - NP & Co-NP

theorycover

Contents

  1. What Is Computable?
  2. Finite State Machines
  3. What is a Turing Machine? 
  4. Computational Complexity
  5. Non-computable numbers
  6. The Transfinite
  7. Axiom Of Choice
  8. Lambda Calculus
  9. Grammar and Torture
  10. Reverse Polish Notation - RPN
  11. Introduction to Boolean Logic
  12. Confronting The Unprovable - Gödel And All That
  13. The Programmer's Guide to Fractals
  14. The Programmer's Guide to Chaos*
  15. Prime Numbers And Primality Testing
  16. Cellular Automata - The How and Why
  17. Information Theory
  18. Coding Theory
  19. Kolmogorov Complexity

*To be revised

picobook

 



 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

<ASIN:1848001207>

<ASIN:0465030793>

<ASIN:193435645X>

<ASIN:0123745144>



Last Updated ( Thursday, 16 May 2019 )