Statement binary operator needs an expression on the

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Any combina3on of numbers is a valid expressions   Rule 2: ARG1 OP ARG2, where each ARG is a valid expression and OP is a binary operator, is a valid expression   Rule 3: – ARG, where ARG is a valid expression 0123290 Valid statement: combina3on of numbers 23÷5+7 ÷5×3 Valid statement: binary operators with valid expressions on both sides Invalid statement: binary operator ÷ needs an expression on the leX! 7 Syntax and Parsing • Parsing means figuring out if the statement is valid (i.e., conforms with the rules of the language)   It is oXen done in a padern ­matching approach; we match parts of the statement to the rules R2: binary operator ÷ R2: binary operator + 23÷5 23 5 R1: combina3on of numbers R1: combina3on of numbers 23÷5+7 7 R1: combina3on of numbers 8 Syntax and Parsing • A statement is invalid if we cannot find any combina3on of rules that “cover” all components of the statement • We say that an invalid statements contains a SyntacAc Error R2: binary operator × R2: binary operator ÷ 5 ×3 5 3 R1: combina3on of numbers R1: combina3on of numbers ÷5×3 ???? MISSING EXPRESSION! 9 3 13 ­09 ­10 Syntax and Parsing • Another SyntacAc Error R2: binary operator × R2: binary operator ÷ 5 ×3 5 3 R1: combina3on of numbers R1: combina3on of numbers cat÷5×3 cat DOES NOT MATCH ANY RULE!!! 10 Programming is a Form of Expression • It is important to learn the syntax rules of a language, and understand how statements are parsed   Figuring out what valid statements look like   Figuring out what valid statements mean • Formal languages are typically harder to read than natural languages   It takes prac3ce to read programs well—work on your code literacy! • One strength of Python is its clean and readable syntax! 11 A Python program is a sequence of statements • Each line in the program is a statement ...
View Full Document

This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online