Programming Languages

Size: px
Start display at page:

Download "Programming Languages"

Transcription

1 Programming Languages Chapter 3 Describing Syntax and Semantics Instructor: Chih-Yi Chiu 邱志義

2 Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics 2

3 Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units For example, the syntax of a Java while statement is while (<boolean_expr>) <statement> the semantics is when the current value of the Boolean expression is true, the statement is executed; otherwise control continues after the while construct 3

4 The General Problem of Describing Syntax Terminology A sentence (statement) is a string of characters over some alphabet A language is a set of sentences A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) A token is a category of lexemes (e.g., identifier, punctuation) 4

5 The General Problem of Describing Syntax Consider the statement: index = 2 * count + 17; Lexemes index Tokens identifier = equal_sign 2 int_literal * mult_op count identifier + plus_op 17 int_literal ; semicolon 5

6 Language Recognizers A recognition device reads input strings of the language and decides whether the input strings belong to the language Example: the syntax analysis part of a compiler Detailed discussion in Chapter 4 6

7 Language Generators A device that generates sentences of a language One can determine if the syntax of a particular sentence is correct by comparing it to the structure of the generator 7

8 Formal Methods of Describing Syntax Backus-Naur Form (BNF) and Context- Free Grammars Most widely known method for describing programming language syntax Extended BNF Improves readability and writability of BNF Grammars and Recognizers 8

9 BNF and Context-Free Grammars Context-free grammars One of grammar classes developed by Noam Chomsky in the mid-1950s The forms of the tokens of programming language can be described by regular grammars The syntax of whole programming language can be described by context-free grammars 9

10 Origins of Backus-Naur Form Backus-Naur Form (1959) Invented by John Backus to describe ALGOL 58; slightly modified by Peter Naur to describe ALGOL 60 BNF is equivalent to context-free grammars In this chapter, the context-free grammar is referred as grammar or BNF 10

11 BNF Fundamentals BNF is a metalanguage used to describe another language In BNF, abstractions are used to represent classes of syntactic structures they act like syntactic variables (also called nonterminal symbols) 11

12 BNF Fundamentals Non-terminals: BNF abstractions Terminals: lexemes and tokens Grammar: a collection of rules 12

13 BNF Rules Examples <assign> <var> = <expression> <if_stmt> if <logic_expr> then <stmt> <ident_list> identifier identifier, <ident_list> 13

14 BNF Rules Left-hand side (LHS): the abstraction being defined Right-hand side (RHS): the definition of the LHS, consisting of mixture of tokens, lexemes, and references to other abstractions A rule has a left-hand side (LHS) and a righthand side (RHS), and consists of terminal and nonterminal symbols A grammar is a finite nonempty set of rules An abstraction (or nonterminal symbol) can have more than one RHS 14

15 Describing Lists Syntactic lists are described using recursion <ident_list> ident ident, <ident_list> A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) 15

16 An Example Grammar 16

17 Derivation <program> => begin <stmt_list> end => begin <stmt> end => begin <var> = <expression> end => begin A = <expression> end => begin A = <var> + <var> end => begin A = B + <var> end => begin A = B + C end 17

18 Derivation Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost 18

19 Another Example How to generate the statement by the leftmost derivation? A = B * (A + C) 19

20 Parse Tree A hierarchical representation of a derivation Try A = A + (B * (C)) 20

21 Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees 21

22 Ambiguity in Grammars 22

23 23

24 Ambiguity in Grammars It is mathematically not possible to determine whether an arbitrary grammar is ambiguous In many cases, an ambiguous grammar can be rewritten to be unambiguous but still generate the desired language 24

25 Operator Precedence When an expression includes two different operators, for example, x + y * z, a semantic issue is the order of evaluation of the two operators 25

26 Unambiguous Grammar Write down the derivation of the sentence A = B + C * A in leftmost and rightmost 26

27 27

28 Associativity of Operators Definition: when an expression includes two operators that have the same precedence, for example A / B * C, a semantic rule is required to specify which should have precedence 28

29 Left associative 29

30 Associativity of Operators In mathematics, addition is associative, i.e., (A + B) + C = A + (B + C) Floating-point addition is not necessarily associative 30

31 Associativity of Operators Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> const (ambiguous) <expr> -> <expr> + const const (unambiguous) <expr> <expr> + const <expr> + const const 31

32 Associativity of Operators When a grammar rule has its LHS also appearing at the beginning of its RHS, the rule is said to be left recursive, which specifies left associativity Unfortunately, left recursion disallows the use of some syntax analysis algorithms Fortunately, left associativity can be enforced by the compiler 32

33 Associativity of Operators In most languages, the exponentiation operator is right associative To indicate right associativity, right recursion can be used A grammar rule is right recursive if the LHS appears at the right end of the RHS <factor> -> <exp> ** <factor> <exp> <exp> -> (<expr>) id For example, a**b**c 33

34 An Unambiguous Grammar for if-then-else Consider the grammar <if_stmt> if <logic_expr> then <stmt> if <logic_expr> then <stmt> else <stmt> <stmt> <if_stmt> This grammar is ambiguous! Write down the possible parse trees of this sentential form if <logic_expr> then if <logic_expr> then <stmt> else <stmt> 34

35 35

36 An Unambiguous Grammar for if-then-else <stmt> -> <matched> <unmatched> <matched> -> if <logic_expr> then <matched> else <matched> any non-if statement <unmatched> -> if <logic_expr> then <stmt> if <logic_expr> then <matched> else <unmatched> Write down the parse tree of this sentential form using the above grammar if <logic_expr> then if <logic_expr> then <stmt> else <stmt> 36

37 Extended BNF Optional parts are placed in brackets [ ] <selection> -> if (<expression>) <statement> [else <statement>] Alternative parts of RHSs are placed inside parentheses ( ) and separated via vertical bars <term> <term> (+ -) const Repetitions (0 or more) are placed inside braces { } <ident_list> <identifier> {, <identifier>} 37

38 Extended BNF The brackets, braces, and parentheses are metasymbols, which means they are notational tools and not terminal symbols in the syntactic entities 38

39 39

40 Grammars and Recognizers Given a context-free grammar, a recognizer for the language generated by the grammar can be algorithmically constructed A number of software systems have been developed that perform this construction One of the first is yacc 40

41 Attribute Grammars Context-free grammars (CFGs) cannot describe all of the syntax of programming languages An attribute grammar is an extension to CFGs to carry some semantic info along parse trees Primary value of attribute grammars (AGs) Static semantics specification Compiler design (static semantics checking) 41

42 Static Semantics Consider type compatibility In Java, a floating-point value cannot be assigned to an integer type variable, although the opposite is legal Specifying this restriction in BNF requires additional nonterminal symbols and rules If all of the typing rules of Java were specified in BNF, the grammar would become too large to be useful 42

43 Static Semantics Consider variable declaration If all variables must be declared before they are referenced, it has been proven this rule cannot be specified in BNF 43

44 Static Semantics Many static semantic rules of a language state its type constraints Static semantics is so named because the analysis required to check these specifications can be done at compile time 44

45 Static Semantics Due to the problems of describing static semantic with BNF, attribute grammars was designed by Knuth to describe both syntax and the static semantics of programs Attribute grammars are a formal approach to both describing and checking the correctness of the static semantics rules of a program 45

46 Basic Concepts of Attribute Grammas Attribute grammas are context-free grammars Attributes are associated grammar symbols; similar to variables Attribute computation functions are associated with grammar rules Predicate functions state the static semantic rules of the language 46

47 Attribute Grammars Defined Let X 0 X 1... X n be a rule, and A(X) be the attribute set of symbol X Functions of the form S(X 0 ) = f(a(x 1 ),..., A(X n )) define synthesized attributes, which pass semantic information up a parse tree Functions of the form I(X j ) = f(a(x 0 ),..., A(X n )), for i j n, define inherited attributes, which pass semantic information down and across a tree 47

48 Attribute Grammars Defined A predicate function has the form of a Boolean expression on the union of the attribute set {(A(X 0 ),..., A(X n )} and a set of literal attribute values If all the attribute values in a parse tree have been computed, the tree is said to be fully attributed 48

49 Intrinsic Attributes Intrinsic attributes are synthesized attributes of leaf nodes whose values are determined outside the parse tree (e.g., symbol table) Initially, in an unattributed parse tree, the only attributes with values are the intrinsic attributes of the leaf nodes Next, the semantic functions can be used to compute the remaining attribute values 49

50 Examples of Attribute Grammars Consider the name on the end of an Ada procedure must match the procedure s name Syntax rule: <proc_def> procedure <proc_name>[1] <proc_body> end <proc_name>[2]; Predicate: <proc_name>[1].string == <proc_name>[2].string 50

51 Examples of Attribute Grammars Let s check the type rules of a simple assignment statement The only variable names are A, B, and C The variables can be one of two types: int or real The type of the expression when the operand types are not the same is always real; when they are the same, the expression type is that of the operands The types of operands in the right side can be mixed, but the assignment is valid only if the LHS and the value resulting from evaluating the RHS have the same type 51

52 Syntax <assign> <var> = <expr> <expr> <var> + <var> <var> <var> A B C actual_type: a synthesized attribute for <var> and <expr>; it is used to store the actual type, int or real, of a variable or expression For variable, the actual type is intrinsic For expression, it is determined from the actual types of the child node expected_type: an inherited attribute for <expr>; it is used to store the type, either int or real, that is expected for the expression, as determined by the type of the variable on the left side of the assignment statement 52

53 53

54 Decorating the Parse Tree 54

55 Decorating the Parse Tree How are attribute values computed? If all attributes were inherited, the tree could be decorated in top-down order If all attributes were synthesized, the tree could be decorated in bottom-up order In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used 55

56 1. <var>.actual_type look-up(a) (Rule 4) 2. <expr>.expected_type <var>.actual_type (Rule 1) 3. <var>[2].actual_type look-up(a) (Rule 4) <var>[3].actual_type look-up(b) (Rule 4) 4. <expr>.actual_type either int or real (Rule 2) 5. <expr>.actual_type == <expr>.expected_type is either TRUE or FALSE (Rule 2) 56

57 A Fully Attributed Parse Tree 57

58 Evaluation Checking the static semantic rules of a language is an essential part of all compilers The main difficult in using an attribute grammar to describe all of the syntax and static semantics is its size and complexity 58

59 Dynamic Semantics About the meaning of expressions, statements, and program units There is no single widely acceptable notation or formalism for describing semantics Operational semantics Axiomatic semantics Denotational semantics 59

60 Operational Semantics Describe the meaning of a statement or program by translating it into a more easily understood language; this language is at the intermediate level Natural operational semantics (the highest level) The final result of the execution of a complete program Structural operational semantics (the lowest level) The precise meaning of a single statement, often by examination of the translated version of the statement 60

61 The Basic Process C statement for (expr1; expr2; expr3;) { } Operational Semantics expr1; loop: if expr2 == 0 goto out expr3; goto loop out: 61

62 Operational Semantics A list of statements in the low-level language ident = var ident = ident + 1 ident = ident 1 goto label if var relop var goto label // relop = {=, <>, >, < >=, <=} ident = var bin_op var ident = un_op var // un_op = unary operator These statements are all simple and therefore easy to understand and implement // bin_op = binary arithmetic operator 62

63 Evaluation Operational semantics provides an effective means of describing semantics for language users and language implementors The statements of one programming language are described in terms of the statements of a lower-level programming language This approach can lead to circularities, where concepts are indirectly defined in terms of themselves The other two approaches are based on logic and mathematics, not programming language 63

64 Axiomatic Semantics Based on formal logic (predicate calculus) Original purpose: formal program verification The logic expressions are called assertions The semantic of a statement is defined by the statement s effect on assertions about the data being processed by the statement 64

65 Assertions Precondition: an assertion immediately preceding a program statement describes the constraints on the program variables at that point Postcondition: an assertion immediately following a statement describes the new constraints on those variables after execution of the statement Weakest precondition: the least restrictive precondition that will guarantee the postcondition 65

66 Assertions Pre-, post form: {P} statement {Q} An example a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0} 66

67 Program Verification 1) A program proof is begun by using the desired results of the program s execution as the postcondition of the last statement of the program 2) The postcondition is used to compute the weakest precondition for the last statement 3) This precondition is then used as the postcondition for the second last statement 4) The process continues until the beginning of the program is reached 5) The precondition of the first statement states the conditions under which the program will compute the desired results 6) If these conditions are implied by the input specification, the program is verified to be correct 67

68 Inference Rule A method of inferring the truth of one assertion on the basis of the values of other assertions General form: S 1, S2,..., Sn S If S1, S2,, and Sn (antecedent) are true, then the truth of S (consequent) can be inferred 68

69 Axiom An axiom is a logical statement that is assumed to be true An axiom is an inference rule without an antecedent 69

70 Assignment Statement Let x = E be a general assignment statement and Q be its postcondition Its precondition P is defined by the axiom P = Q x E It means P is computed as Q with all instances of x replaced by E 70

71 Assignment Statement Example: a = b / 2 1 {a < 10} The weakest precondition is computed as: b / 2-1 < 10 b < 22 Thus, the weakest precondition for the given assignment and postcondition is {b < 22} The axiom for the assignment statement (x = E): {Q x->e } x = E {Q} 71

72 Assignment Statement A given assignment statement with a precondition and a postcondition can be considered a theorem If the assignment axiom produces the given precondition, the theorem is proved 72

73 Assignment Statement Example {x > 3} x = x 3 {x > 0} Using the assignment axiom on x = x 3 { x > 0} produces {x > 3}, which is the given precondition Therefore, we have proven the example logical statement 73

74 Assignment Statement How to prove this statement: {x > 5} x = x 3 {x > 0} We need an inference rule, named the rule of consequence 74

75 Rule of Consequence The general form {P} S{Q},P' P, Q {P'}S{Q'} Q' means implies A postcondition can always be weakened and a precondition can always be strengthened 75

76 Rule of Consequence How to prove this statement: {x > 5} x = x 3 {x > 0} Use the rule of consequence {P} S{Q},P' P, Q {P'}S{Q'} Q' we prove the consequent is true: {x 3}x x - 3{x 0},{x 5} {x {x 5}x x - 3{x 3},{x 0} 0} {x 0} 76

77 Sequences Suppose we have the following adjacent statements {P1} S1 {P2} {P2} S2 {P3} An inference rule for sequences {P1}S1{P2},{P2}S2 {P3} {P1}S1; S2 {P3} 77

78 Sequences If S1 and S2 are assignment statements S1: x1 = E1 S2: x2 = E2 Then we have {P3 x2 E2 } x2 = E2 {P3} {(P3 x2 E2 ) x1 E1 } x1 = E1 {P3 x2 E2 } The weakest precondition for the sequence with postcondition P3 is {(P3 x2 E2 ) x1 E1 } 78

79 Sequences Example: y = 3 * x + 1; x = y + 3; {x < 10} The precondition for the last assignment statement is {y < 7}, which is used as the postcondition for the first statement. The first statement s precondition is {x < 2}. 79

80 Selection The general form of selection statements is if B then S1 else S2 The inference rule is {Band P}S1{Q},{(not B) and P}S2 {Q} {P}if B then S1 else S2 {Q} It indicates selection statements must be proven for both conditions 80

81 Selection Example if (x > 0) y = y 1 else y = y + 1 {y > 0} The precondition for the then clause is {y > 1}, while the precondition for the else clause is {y > -1}. Because {y > 1} {y >-1}, the rule of consequence allow us to use {y > 1} for precondition of the selection statement. 81

82 Logical Pretest Loops An inference rule for logical pretest loops {P} while B do S end {Q} {I and B}S{I} {I}while B do S{I and (not B)} where I is the loop invariant (the inductive hypothesis). The complexity lies in finding an appropriate loop invariant. 82

83 Logical Pretest Loops The axiomatic description of a while loop is written as {P} while B do S end {Q} 83

84 Logical Pretest Loops The complete axiomatic description of a while construct requires all of the following to be true (the selection of I must meet the following conditions): P => I -- the loop invariant must be true initially {I and B} S {I} -- I is not changed by executing the body of the loop (I and (not B)) => Q -- if I is true and B is false, Q is implied The loop terminates -- the loop must be terminated 84

85 Logical Pretest Loops The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition 85

86 Logical Pretest Loops Example 1: while y x do y = y + 1 end {y = x} For zero iteration, the weakest precondition is {y = x} For one iteration, it is wp(y = y + 1, {y = x}) = {y + 1 = x} or {y = x 1} where wp(statement, postcondition) = weak precondition 86

87 Logical Pretest Loops For two iterations, it is wp(y = y + 1, {y = x - 1}) = {y + 1 = x 1}, or {y = x 2} For three iterations, it is wp(y = y + 1, {y = x - 2}) = {y + 1 = x 2}, or {y = x 3} It is obvious {y < x} will suffice for one or more iterations; combining with {y = x} for the zero iteration, we get {y x}, which can be used for the loop invariant. 87

88 Logical Pretest Loops To make sure the four criteria to be true Because P = I, we have P I {I and B} S {I}. Proof: {y x and y x} y = y + 1 {y x} {I and (not B)} Q. Proof: {y x and y = x} {y = x} Loop termination. The question is whether the loop terminates: {y x} while y x do y = y + 1 end {y = x} 88

89 Logical Pretest Loops Example 2 while s > 1 do s = s / 2 end {s = 1} For zero iteration, the weakest precondition is {s = 1} For one iteration, it is wp(s = s / 2, {s = 1}) = {s / 2 = 1}, or {s = 2} For two iterations, it is wp(s = s / 2, {s = 2}) = {s / 2 = 2}, or {s = 4} 89

90 Logical Pretest Loops We can see the invariant is {s is a nonnegative power of 2} However, this is not a weakest precondition Consider using the precondition {s > 1}. Can the following logical statement be proven? {s > 1} while s > 1 do s = s / 2 end {s = 1} 90

91 Logical Pretest Loops It can not be proven using the precondition {s > 1} Violate rules 2 & 3 What do you think the invariant? 91

92 Logical Pretest Loops Finding loop invariants is not always easy It is often ignore the requirement of loop termination condition because its difficulty If loop termination can be shown, the axiomatic description of the loop is called total correctness If the other conditions can be met but termination is not guaranteed, it is called partial correctness 92

93 Program Proof Example: {x = A AND y = B} t = x; x = y; y = t; {x = B AND y = A} Use the assignment axiom and the inference rule for sequences for proof. 93

94 Program Proof Example: 1. {n 0} 2. count = n; 3. fact = 1; 4. while count 0 do 5. fact = fact * count; 6. count = count 1; 7. end 8. {fact = n!} 94

95 Let I = (fact = (count+1)* * n) & (count 0) (1) P => I (2) {I and B} S {I} {I and B} is ((fact = (count+1)* * n) & (count 0)) & (count 0) = ((fact = (count+1)* * n) & (count>0)) In line 6, {P} count = count-1 {I} {P} is {(fact = count*(count+1)* *n) & (count >= 1)} In line 5, {P} fact = fact*count {(fact = count*(count+1)* *n) & (count >= 1)} {P} is {(fact = (count+1)* *n) & (count >= 1)} same to {I and B} 95

96 (3) I & (NOT B) => Q (fact = (count+1)* *n) & (count>=0)) & (count=0)) fact = n! We have proven the choice of I meets the requirements for a loop invariant! In line 3, {P} fact = 1 {fact = (count+1)* *n) & (count >= 0)} {P} is (1 = (count+1)* *n) & (count >= 0)) In line 2, {P} count = n {(1=(count+1)* *n) & (count >= 0)) {P} to be {(n+1)* *n = 1) & (n >=0)}?? 96

97 Evaluation of Axiomatic Semantics Developing axioms or inference rules for all of the statements in a language is difficult It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers Its usefulness in describing the meaning of a programming language is limited for language users or compiler writers 97

98 Denotational Semantics Denotational semantics is the most rigorous, widely known method for describing the meaning of programs Based on recursive function theory The most abstract semantics description method Originally developed by Scott and Strachey (1970) 98

99 Denotational Semantics The process of building a denotational specification for a language Define a mathematical object for each language entity Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects The meaning of language constructs are defined by only the values of the program's variables 99

100 Denotation Semantics vs. Operational Semantics In operational semantics, the state changes are defined by coded algorithms In denotational semantics, the state changes are defined by rigorous mathematical functions 100

101 Denotational Semantics Example I The syntax of binary numbers <bin_num> 0 1 <bin_num> 0 <bin_num> 1 101

102 Denotational Semantics Example I The semantic function, named M bin, maps the syntactic objects (described in grammar rules) to the objects in N M bin ( 0 ) = 0 M bin ( 1 ) = 1 M bin (<bin_num> 0 ) = 2 * M bin (<bin_num>) M bin (<bin_num> 1 ) = 2 * M bin (<bin_num>)

103 Denotational Semantics Example I The meanings, or denoted objects, can be attached to the nodes of the parse tree 103

104 Denotational Semantics Example II The syntax of decimal literals <dec_num> <dec_num> ( ) The denotational mappings are M dec ( 0 ) = 0, M dec ( 1 ) = 1,, M dec ( 9 ) = 9 M dec (<dec_num> 0 ) = 10 * M dec (<dec_num>) M dec (<dec_num> 1 ) = 10 * M dec (<dec_num>) + 1 M dec (<dec_num> 9 ) = 10 * M dec (<dec_num>)

105 The State of a Program The state s of a program is the values of all its current variables s = {<i 1, v 1 >, <i 2, v 2 >,, <i n, v n >} Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(i j, s) = v j 105

106 Expressions We assume expressions are decimal numbers, variables, or binary expressions having one arithmetic operator and two operands, each of which can be an expression BNF syntax <expr> <dec_num> <var> <binary_expr> <binary_expr> <left_expr> <operator> <right_expr> <left_expr> <dec_num> <var> <right_expr> <dec_num> <var> <operator> + * 106

107 M e (<expr>, s) = case <expr> of <dec_num> => M dec (<dec_num>, s) <var> => if VARMAP(<var>, s) == undef error else VARMAP(<var>, s) <binary_expr> => if (M e (<binary_expr>.<left_expr>, s) == undef OR M e (<binary_expr>.<right_expr>, s) = undef) then error else if (<binary_expr>.<operator> == + M e (<binary_expr>.<left_expr>, s) + M e (<binary_expr>.<right_expr>, s) else M e (<binary_expr>.<left_expr>, s) * M e (<binary_expr>.<right_expr>, s) 107

108 Assignment Statements An assignment statement is an expression evaluation plus the setting of the left-side variable to the expression s value In this case, the meaning function maps a state to a state 108

109 M a (x = E, s) = if M e (E, s) == error error else s = {<i 1,v 1 >,<i 2,v 2 >,...,<i n,v n >}, where for j = 1, 2,..., n, if i j == x v j = M e (E, s) else v j = VARMAP(i j, s) 109

110 Logical Pretest Loops Using two other mapping functions M sl : maps statement lists to states M b : maps Boolean expressions to Boolean values (or error) 110

111 M l (while B do L, s) = if M b (B, s) == undef error else if M b (B, s) == false s else if M sl (L, s) == error error else M l (while B do L, M sl (L, s)) 111

112 Loop Meaning In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions Compared to iteration, recursion is easier to describe with mathematical rigor 112

113 Evaluation Can be used to prove the correctness of programs Provides a rigorous way to think about programs Can be an aid to language design Has been used in compiler generation systems Because of its complexity, they are of little use to language users 113

114 Summary BNF and context-free grammars are equivalent meta-languages Well-suited for describing the syntax of programming languages An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language Three primary methods of semantics description Operation, axiomatic, denotational 114

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

3. DESCRIBING SYNTAX AND SEMANTICS

3. DESCRIBING SYNTAX AND SEMANTICS 3. DESCRIBING SYNTAX AND SEMANTICS CSc 4330/6330 3-1 9/15 Introduction The task of providing a concise yet understandable description of a programming language is difficult but essential to the language

More information

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Who must use language definitions? 1. Other language

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019 Chapter 3. Topics The terms of Syntax, Syntax Description Method: Context-Free Grammar (Backus-Naur Form) Derivation Parse trees Ambiguity Operator precedence and associativity Extended Backus-Naur Form

More information

Syntax and Semantics

Syntax and Semantics Syntax and Semantics Syntax - The form or structure of the expressions, statements, and program units Semantics - The meaning of the expressions, statements, and program units Syntax Example: simple C

More information

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Describing the Meanings of Programs: Dynamic Semantics Copyright 2015 Pearson. All rights reserved. 2 Semantics There is no

More information

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999 Chapter 3: Syntax and Semantics Matt Evett Dept. Computer Science Eastern Michigan University 1999 Syntax and Semantics Syntax - the form or structure of the expressions, statements, and program units

More information

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics There is no single widely acceptable notation or formalism for describing semantics Operational Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The

More information

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings

More information

Describing Syntax and Semantics

Describing Syntax and Semantics Describing Syntax and Semantics Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units Syntax

More information

Chapter 3. Describing Syntax and Semantics. Introduction. Why and How. Syntax Overview

Chapter 3. Describing Syntax and Semantics. Introduction. Why and How. Syntax Overview Chapter 3 Describing Syntax and Semantics CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 1 Introduction We usually break down the problem of defining a programming language into two parts.

More information

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield CMPS 3500 Programming Languages Dr. Chengwei Lei CEECS California State University, Bakersfield Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing

More information

Syntax. In Text: Chapter 3

Syntax. In Text: Chapter 3 Syntax In Text: Chapter 3 1 Outline Syntax: Recognizer vs. generator BNF EBNF Chapter 3: Syntax and Semantics 2 Basic Definitions Syntax the form or structure of the expressions, statements, and program

More information

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Who must use language definitions? 1. Other language

More information

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process Dynamic Semantics DYNAMIC SEMANTICS Describe the meaning of expressions, statements, and program units No single widely acceptable notation or formalism for describing semantics Two common approaches:

More information

P L. rogramming anguages. Fall COS 301 Programming Languages. Syntax & Semantics. UMaine School of Computing and Information Science

P L. rogramming anguages. Fall COS 301 Programming Languages. Syntax & Semantics. UMaine School of Computing and Information Science COS 301 P L Syntax & Semantics Syntax & semantics Syntax: Defines correctly-formed components of language Structure of expressions, statements Semantics: meaning of components Together: define the p language

More information

Syntax & Semantics. COS 301 Programming Languages

Syntax & Semantics. COS 301 Programming Languages COS 301 P L Syntax & Semantics Syntax & semantics Syntax: Defines correctly-formed components of language Structure of expressions, statements Semantics: meaning of components Together: define the p language

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics. Outline Semantic Analysis In Text: Chapter 3 Static semantics Attribute grammars Dynamic semantics Operational semantics Denotational semantics N. Meng, S. Arthur 2 Syntax vs. Semantics Syntax concerns

More information

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Copyright 2009 Addison-Wesley. All

More information

Defining Languages GMU

Defining Languages GMU Defining Languages CS463 @ GMU How do we discuss languages? We might focus on these qualities: readability: how well does a language explicitly and clearly describe its purpose? writability: how expressive

More information

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG Attribute Evaluation Order Determining attribute 2 expected_type evaluation order actual_type actual_type for any attribute grammar is a 5 complex problem, 1 [1] [2] requiring

More information

Programming Language Syntax and Analysis

Programming Language Syntax and Analysis Programming Language Syntax and Analysis 2017 Kwangman Ko (http://compiler.sangji.ac.kr, kkman@sangji.ac.kr) Dept. of Computer Engineering, Sangji University Introduction Syntax the form or structure of

More information

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor COSC252: Programming Languages: Semantic Specification Jeremy Bolton, PhD Adjunct Professor Outline I. What happens after syntactic analysis (parsing)? II. Attribute Grammars: bridging the gap III. Semantic

More information

Programming Language Definition. Regular Expressions

Programming Language Definition. Regular Expressions Programming Language Definition Syntax To describe what its programs look like Specified using regular expressions and context-free grammars Semantics To describe what its programs mean Specified using

More information

Syntax. A. Bellaachia Page: 1

Syntax. A. Bellaachia Page: 1 Syntax 1. Objectives & Definitions... 2 2. Definitions... 3 3. Lexical Rules... 4 4. BNF: Formal Syntactic rules... 6 5. Syntax Diagrams... 9 6. EBNF: Extended BNF... 10 7. Example:... 11 8. BNF Statement

More information

Program Assignment 2 Due date: 10/20 12:30pm

Program Assignment 2 Due date: 10/20 12:30pm Decoration of parse tree for (1 + 3) * 2 N. Meng, S. Arthur 1 Program Assignment 2 Due date: 10/20 12:30pm Bitwise Manipulation of Hexidecimal Numbers CFG E E A bitwise OR E A A A ^ B bitwise XOR A B B

More information

Habanero Extreme Scale Software Research Project

Habanero Extreme Scale Software Research Project Habanero Extreme Scale Software Research Project Comp215: Grammars Zoran Budimlić (Rice University) Grammar, which knows how to control even kings - Moliere So you know everything about regular expressions

More information

Unit-1. Evaluation of programming languages:

Unit-1. Evaluation of programming languages: Evaluation of programming languages: 1. Zuse s Plankalkül 2. Pseudocodes 3. The IBM 704 and Fortran 4. Functional Programming: LISP 5. The First Step Toward Sophistication: ALGOL 60 6. Computerizing Business

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction Topics Chapter 3 Semantics Introduction Static Semantics Attribute Grammars Dynamic Semantics Operational Semantics Axiomatic Semantics Denotational Semantics 2 Introduction Introduction Language implementors

More information

ICOM 4036 Spring 2004

ICOM 4036 Spring 2004 Language Specification and Translation ICOM 4036 Spring 2004 Lecture 3 Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-1 Language Specification and Translation Topics Structure of a Compiler

More information

COP 3402 Systems Software Syntax Analysis (Parser)

COP 3402 Systems Software Syntax Analysis (Parser) COP 3402 Systems Software Syntax Analysis (Parser) Syntax Analysis 1 Outline 1. Definition of Parsing 2. Context Free Grammars 3. Ambiguous/Unambiguous Grammars Syntax Analysis 2 Lexical and Syntax Analysis

More information

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3 Programming Language Specification and Translation ICOM 4036 Fall 2009 Lecture 3 Some parts are Copyright 2004 Pearson Addison-Wesley. All rights reserved. 3-1 Language Specification and Translation Topics

More information

CPS 506 Comparative Programming Languages. Syntax Specification

CPS 506 Comparative Programming Languages. Syntax Specification CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens

More information

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser

More information

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter

More information

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1 Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1 1. Introduction Parsing is the task of Syntax Analysis Determining the syntax, or structure, of a program. The syntax is defined by the grammar rules

More information

Programming Languages, Summary CSC419; Odelia Schwartz

Programming Languages, Summary CSC419; Odelia Schwartz Programming Languages, Summary CSC419; Odelia Schwartz Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design

More information

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF) Chapter 3: Describing Syntax and Semantics Introduction Formal methods of describing syntax (BNF) We can analyze syntax of a computer program on two levels: 1. Lexical level 2. Syntactic level Lexical

More information

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters : Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Scanner Parser Static Analyzer Intermediate Representation Front End Back End Compiler / Interpreter

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction Topics Chapter 3 Semantics Introduction Static Semantics Attribute Grammars Dynamic Semantics Operational Semantics Axiomatic Semantics Denotational Semantics 2 Introduction Introduction Language implementors

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Crafting a Compiler with C (II) Compiler V. S. Interpreter Crafting a Compiler with C (II) 資科系 林偉川 Compiler V S Interpreter Compilation - Translate high-level program to machine code Lexical Analyzer, Syntax Analyzer, Intermediate code generator(semantics Analyzer),

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

CS323 Lecture - Specifying Syntax and Semantics Last revised 1/16/09

CS323 Lecture - Specifying Syntax and Semantics Last revised 1/16/09 CS323 Lecture - Specifying Syntax and Semantics Last revised 1/16/09 Objectives: 1. To review previously-studied methods for formal specification of programming language syntax, and introduce additional

More information

Part 5 Program Analysis Principles and Techniques

Part 5 Program Analysis Principles and Techniques 1 Part 5 Program Analysis Principles and Techniques Front end 2 source code scanner tokens parser il errors Responsibilities: Recognize legal programs Report errors Produce il Preliminary storage map Shape

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

A programming language requires two major definitions A simple one pass compiler

A programming language requires two major definitions A simple one pass compiler A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:

More information

Languages and Compilers

Languages and Compilers Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 2012-13 3. Formal Languages, Grammars and Automata Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office:

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

More information

EECS 6083 Intro to Parsing Context Free Grammars

EECS 6083 Intro to Parsing Context Free Grammars EECS 6083 Intro to Parsing Context Free Grammars Based on slides from text web site: Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. 1 Parsing sequence of tokens parser

More information

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing Syntax/semantics Program program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing Meta-models 8/27/10 1 Program program execution Syntax Semantics

More information

Formal Languages. Formal Languages

Formal Languages. Formal Languages Regular expressions Formal Languages Finite state automata Deterministic Non-deterministic Review of BNF Introduction to Grammars Regular grammars Formal Languages, CS34 Fall2 BGRyder Formal Languages

More information

PRINCIPLES OF PROGRAMMING LANGUAGES

PRINCIPLES OF PROGRAMMING LANGUAGES LECTURE NOTES ON PRINCIPLES OF PROGRAMMING LANGUAGES III B.TECH CSE I SEMESTER (JNTUA-R15) Ms. K.PRATHIMA M.Tech., Assistant Professor DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CHADALAWADA RAMANAMMA

More information

Principles of Programming Languages COMP251: Syntax and Grammars

Principles of Programming Languages COMP251: Syntax and Grammars Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2006

More information

Syntax Analysis Check syntax and construct abstract syntax tree

Syntax Analysis Check syntax and construct abstract syntax tree Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error reporting and recovery Model using context free grammars Recognize using Push down automata/table Driven Parsers

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott Introduction programming languages need to be precise natural languages less so both form (syntax) and meaning

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and context-free grammars Grammar derivations More about parse trees Top-down and bottom-up

More information

CSCI312 Principles of Programming Languages!

CSCI312 Principles of Programming Languages! CSCI312 Principles of Programming Languages! Chapter 2 Syntax! Xu Liu Review! Principles of PL syntax, naming, types, semantics Paradigms of PL design imperative, OO, functional, logic What makes a successful

More information

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer: Theoretical Part Chapter one:- - What are the Phases of compiler? Six phases Scanner Parser Semantic Analyzer Source code optimizer Code generator Target Code Optimizer Three auxiliary components Literal

More information

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA. R13 SET - 1 III B. Tech I Semester Regular Examinations, November - 2015 1 a) What constitutes a programming environment? [3M] b) What mixed-mode assignments are allowed in C and Java? [4M] c) What is

More information

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;} Compiler Construction Grammars Parsing source code scanner tokens regular expressions lexical analysis Lennart Andersson parser context free grammar Revision 2012 01 23 2012 parse tree AST builder (implicit)

More information

This book is licensed under a Creative Commons Attribution 3.0 License

This book is licensed under a Creative Commons Attribution 3.0 License 6. Syntax Learning objectives: syntax and semantics syntax diagrams and EBNF describe context-free grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars

More information

2.2 Syntax Definition

2.2 Syntax Definition 42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee 1 What Is a Programming Language? Language = syntax + semantics The syntax of a language is concerned with the form of a program: how

More information

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols The current topic:! Introduction! Object-oriented programming: Python! Functional programming: Scheme! Python GUI programming (Tkinter)! Types and values! Logic programming: Prolog! Introduction! Rules,

More information

Introduction to Parsing

Introduction to Parsing Introduction to Parsing The Front End Source code Scanner tokens Parser IR Errors Parser Checks the stream of words and their parts of speech (produced by the scanner) for grammatical correctness Determines

More information

Syntax Intro and Overview. Syntax

Syntax Intro and Overview. Syntax Syntax Intro and Overview CS331 Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal

More information

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built) Programming languages must be precise Remember instructions This is unlike natural languages CS 315 Programming Languages Syntax Precision is required for syntax think of this as the format of the language

More information

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications Agenda for Today Regular Expressions CSE 413, Autumn 2005 Programming Languages Basic concepts of formal grammars Regular expressions Lexical specification of programming languages Using finite automata

More information

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad-00 014 Subject: PPL Class : CSE III 1 P a g e DEPARTMENT COMPUTER SCIENCE AND ENGINEERING S No QUESTION Blooms Course taxonomy level Outcomes UNIT-I

More information

Homework & Announcements

Homework & Announcements Homework & nnouncements New schedule on line. Reading: Chapter 18 Homework: Exercises at end Due: 11/1 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 25 COS 140: Foundations of

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Lecture 4: Syntax Specification

Lecture 4: Syntax Specification The University of North Carolina at Chapel Hill Spring 2002 Lecture 4: Syntax Specification Jan 16 1 Phases of Compilation 2 1 Syntax Analysis Syntax: Webster s definition: 1 a : the way in which linguistic

More information

Dr. D.M. Akbar Hussain

Dr. D.M. Akbar Hussain Syntax Analysis Parsing Syntax Or Structure Given By Determines Grammar Rules Context Free Grammar 1 Context Free Grammars (CFG) Provides the syntactic structure: A grammar is quadruple (V T, V N, S, R)

More information

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1 Defining Program Syntax Chapter Two Modern Programming Languages, 2nd ed. 1 Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

More information

Chapter 4. Lexical and Syntax Analysis

Chapter 4. Lexical and Syntax Analysis Chapter 4 Lexical and Syntax Analysis Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing Copyright 2012 Addison-Wesley. All rights reserved.

More information

Context-free grammars (CFG s)

Context-free grammars (CFG s) Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of

More information

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Massachusetts Institute of Technology Language Definition Problem How to precisely define language Layered structure

More information

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Language Definition Problem How to precisely

More information

Principles of Programming Languages COMP251: Syntax and Grammars

Principles of Programming Languages COMP251: Syntax and Grammars Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2007

More information

Wednesday, September 9, 15. Parsers

Wednesday, September 9, 15. Parsers Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs: What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees 3.3.1 Parse trees 1. Derivation V.S. Structure Derivations do not uniquely represent the structure of the strings

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

More information

Defining syntax using CFGs

Defining syntax using CFGs Defining syntax using CFGs Roadmap Last time Defined context-free grammar This time CFGs for specifying a language s syntax Language membership List grammars Resolving ambiguity CFG Review G = (N,Σ,P,S)

More information

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax

More information

CSE302: Compiler Design

CSE302: Compiler Design CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University February 20, 2007 Outline Recap

More information

Parsing II Top-down parsing. Comp 412

Parsing II Top-down parsing. Comp 412 COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

Decaf Language Reference

Decaf Language Reference Decaf Language Reference Mike Lam, James Madison University Fall 2016 1 Introduction Decaf is an imperative language similar to Java or C, but is greatly simplified compared to those languages. It will

More information