23 - Click to edit Master subtitle style Chapter Twenty-...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Twenty- Three Modern Programming Languages, 2nd ed. Formal Semantics Chapter Twenty- Three Modern Programming Languages, 2nd ed. 11 Chapter Twenty- Three Modern Programming Languages, 2nd ed. Formal Semantics At the beginning of the book we saw formal definitions of syntax with BNF And how to make a BNF that generates correct parse trees: where syntax meets semantics We saw how parse trees can be simplified into abstract syntax trees (ASTs) Now the rest of the story: formal definitions of programming language semantics Chapter Twenty- Three Modern Programming Languages, 2nd ed. 22 Chapter Twenty- Three Modern Programming Languages, 2nd ed. Outline Natural semantics and Prolog interpreters Language One Language Two: adding variables Language Three: adding functions Chapter Twenty- Three Modern Programming Languages, 2nd ed. 33 Chapter Twenty- Three Modern Programming Languages, 2nd ed. Defining Language One A little language of integer expressions: Constants The binary infix operators + and * , with the usual precedence and associativity Parentheses for grouping Lexical structure: tokens are + , * , ( , ) , and integer constants consisting of one or more decimal digits Chapter Twenty- Three Modern Programming Languages, 2nd ed. 44 Chapter Twenty- Three Modern Programming Languages, 2nd ed. Syntax: Phrase Structure (A subset of ML expressions, Java expressions, and Prolog terms) This grammar is unambiguous Both operators are left associative, and * has higher precedence than + Chapter Twenty- Three Modern Programming Languages, 2nd ed. 55 < exp > ::= < exp > + < mulexp > | < mulexp > < mulexp > ::= < mulexp > * < rootexp > | < rootexp > < rootexp > ::= ( < exp > ) | < constant > Chapter Twenty- Three Modern Programming Languages, 2nd ed. Parse Trees And ASTs The grammar generates parse trees The AST is a simplified form: same order as the parse tree, but no non-terminals Chapter Twenty- Three Modern Programming Languages, 2nd ed. 66 < exp > < exp > + < mulexp > 3 < mulexp > * < rootexp > 2 1 < mulexp > < rootexp > < rootexp > + 1 2 * 3 Chapter Twenty- Three Modern Programming Languages, 2nd ed. Continuing The Definition That is as far as we got in Chapters 2 and 3 One way to define the semantics of the language is to give an interpreter for it We will write one in Prolog, using ASTs as input: Chapter Twenty- Three Modern Programming Languages, 2nd ed. 77 + 1 2 * 3 plus(const(1),times(const(2),const(3))) Chapter Twenty- Three Modern Programming Languages, 2nd ed. Abstract Syntax Note: the set of legal ASTs can be defined by a grammar, giving the abstract syntax of the language An abstract syntax can be ambiguous, since the order is already fixed by parsing with the original grammar for concrete syntax Chapter Twenty- Three Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 54

23 - Click to edit Master subtitle style Chapter Twenty-...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online