Lesson Objectives Aims 1 Understand Syntax Analysis Syntax
Lesson Objectives Aims 1. Understand Syntax Analysis
Syntax • All languages (human and programming) have rules. • Unlike English, for example, computers are totally unforgiving of bad grammar • “You’re a moron” • “Your a moron” My a moron what? !
Syntax • Syntax is the grammar of a programming language • It is a set of rules that lines of code must adhere to in order to be “syntactically correct” • If a line of code does not adhere to any known rule, a syntax error is generated
Example
Syntax Error • How does the compiler know this is a spelling error? ? dimm chunder As Integer • How would this line of code be interpreted?
• The syntax analyser will check for two things: 1. Spelling errors 2. Whether a token stream is valid and matches a given rule
• The syntax analyser will use rules that are based on some form of logic • One such method is Backus Naur Form • This is a method of tightly defining atomic rules that cannot be misinterpreted • They are also recursive in nature
Abstract Syntax Tree • Once syntax has been verified, an Abstract Syntax Tree is produced • Why: – Unambiguous (tightly defined) data structure – Can only be read in one way – Removes any uncertainty about operator precedence
• 2 * 3 + 4…. Which happens first?
Task • Find out and make notes on: • BNF – why is it used? • BNF – how are numbers represented (recursively(what is recursion? ? )) • AST – what does a simple AST look like? What about for a common statement such as IF?
Review/Success Criteria You should know: ü ü ü What Syntax is What role the syntax analysis stage plays in compilation What will generate a syntax error What an AST is What BNF is and why it is useful in syntax parsing
- Slides: 11