• No results found

WEEK 3. Language Syntax and Semantics ISBN

N/A
N/A
Protected

Academic year: 2021

Share "WEEK 3. Language Syntax and Semantics ISBN"

Copied!
18
0
0

Loading.... (view fulltext now)

Full text

(1)

ISBN 0-321-49362-1

WEEK 3

(2)

Introduction

• Syntax: it is the form of the expressions, statements, and program units

• Semantics: the meaning of the expressions, statements, and program units

(3)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-3

The General Problem of Describing

Syntax: Terminology

• A

sentence

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 that include operators &

special words

(4)

Formal Definition of Languages

• Recognizers

– A recognition device reads input strings of the language and decides whether the input strings belong to the language

– Example: syntax analysis part of a compiler

• Generators

– A device that generates sentences of a language – One can determine if the syntax of a particular

(5)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-5

Formal Methods of Describing

Syntax

• Backus-Naur Form and Context-Free Grammars

• Extended BNF

(6)

BNF and Context-Free Grammars

• Context-Free Grammars

– Developed by Noam Chomsky in the mid-1950s – Language generators are meant to describe the

syntax of natural languages

(7)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-7

Backus-Naur Form (BNF)

• Backus-Naur Form (1959)

– Invented by John Backus to describe Algol 58 – BNF is a metalanguage used to describe

another language

– In BNF, abstractions are used to represent syntactic variables that are called

(8)

BNF Fundamentals

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

– Examples of BNF rules:

<ident_list> → identifier | identifier, <ident_list>

(9)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-9

BNF Rules

• A rule has a left-hand side (LHS) and a right-hand 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

<stmt>  <single_stmt>

(10)

An Example Grammar

<program>  <stmts>

<stmts>  <stmt> | <stmt> ; <stmts> <stmt>  <var> = <expr>

<var>  a | b | c | d

(11)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-11

An Example Derivation

<program> => <stmts> => <stmt>

=> <var> = <expr> => a =<expr> => a = <term> + <term>

=> a = <var> + <term> => a = b + <term>

(12)

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

(13)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-13

Parse Tree

• A hierarchical representation of a derivation

(14)

Ambiguity in Grammars

(15)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-15

An Ambiguous Expression Grammar

<expr>  <expr> <op> <expr> | const <op>  / | -<expr> <expr> <expr> <expr> <expr> <expr> <expr> <expr> <expr> <expr> <op> <op> <op> <op>

const - const / const const - const / const

(16)

• When an expression includes two different

operators, for example, x + y * z, one obvious

semantic issue is the order of evaluation of the two operators (for example, in this expression is it add and then multiply, or vice versa?). This semantic

(17)

Copyright © 2007 Addison-Wesley. All rights reserved. 1-17

An Unambiguous Expression Grammar

• If we use the parse tree to indicate

precedence levels of the operators, we cannot have ambiguity

<expr>  <expr> - <term> | <term> <term>  <term> / const| const

(18)

-Attribute Grammars

• Context-free grammars (CFGs) cannot describe all of the syntax of

programming languages

• Additions to CFGs to carry some semantic info along parse trees

• Primary value of attribute grammars (AGs)

References

Related documents

It attempts to answer a central question; ‘how does the change of our attitude to privacy affect the latent aspects of house design and planning in contemporary

correlation coefficient amplitude exceeds that of the uncorrected data... Correction performance expressed by the mean correlation coefficient amplitude between undisturbed

The UK Market Unit comprises private health insurance, wellbeing services, care homes, out of hospital healthcare services and a complex care hospital in London.. In 2012,

others. • People who feel it is the right time to attend. • People who are willing and able to follow all of the safe sanctuary guidelines and hold others accountable to them.

COSE in family business Differentiation Customer well-being Customer experience Family influence Social skills Decision-making authority Motivation Technical skills RP1 RP2 RP3

more than four additional runs were required, they were needed for the 2 7-3 design, which is intuitive as this design has one more factor than the 2 6-2 design

For Aim 2, numeric data on clinical aspects were collected using several data sources: (1) a validated pain relief item from the Brief Pain Inventory instrument was

Ključni koncept kroz koji ću promatrati proces izgradnje države u Hrvatskoj i Monarhiji, odnosno ulogu Nikole III Erdődyja i Hrvatskog sabora u datom procesu, je odnos