Top Banner

of 17

Part 3 - Syntax, Semantics and Pragmatics

Jul 07, 2018

Download

Documents

John Abaya
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    1/17

    Three aspects of language

    • Syntax is the required grammar and punctuation of the language – Compile-time errors are syntax errors

    • Semantics is all about meaning--what thestatements do, what the programs do – Logic errors are semantic errors

    • Pragmatics has to do with what’s good! andbad! about a language or program

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    2/17

    Syntax examples

    • "#$%$&' statements are one per line(modern languages are free-format

    • Pascal uses semicolons between statements(C uses semicolons after statements

    • Pascal uses begin…end to group statements(C uses { and }

    • Pascal uses the )eyword integer ( C uses int

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    3/17

    Another syntax example

    • C* if (x > y) x = x-1; else y--;Pascal* if x > y then x := x-1 else y := y-1;

    • +ifferences* parentheses around x > y, theword then, = or :=, semicolon before else, --

    • %hese are syntactic differences( the meaningsare identical

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    4/17

    The importance of syntax

    • Correct syntax is ob iously important( if youdon’t get it right, your program won’t run

    • n a sense, syntax is tri ial( you learn it, youfix it until it’s right, end of story

    • .ut the syntax of a language greatly affects* – how easy it is to write programs – how easy it is to read and understand programs – how easy it is to ma)e hard-to-see syntax errors

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    5/17

    Examples of poor syntax

    • n "#$%$&', ariables don’t ha e to bedeclared – %herefore, e ery misspelling is a new ariable

    • n Pascal, semicolons go between statements – %herefore, adding a statement to a bloc)

    in ol es adding a semicolon to the pre ious line

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    6/17

    An example of good syntax

    • n &da, control statements ha e the formif…end if, while…end while, case…endcase , etc/ – %his helps a oid the confusion 0in C1 resulting

    from large groups of anonymous closing braces

    • 2yntax is usually more important forreading and understanding programs thanfor writing them

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    7/17

    Why syntax matters

    • %he C ersion has a bug that almost ne er occurs in &da

    C* if (x < y) te ! = x; x = y; y = te !;

    &da* if x < y then te ! := x; x := y; y := te !

    end if;

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    8/17

    Semantics

    • 2emantics has to do with the meaning ofconstructs in a language, and the meaningsof programs written in that language

    • 2emantics is fundamental to e erything youdo in a language

    • 2yntax is 3ust the code! you use todescribe the semantics

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    9/17

    High-level semantics

    • 2emantics can affect things at a ery highle el* – C is a procedural language( you describe a set

    of procedures to follow – 4a a is an ob3ect-oriented language( you

    describe ob3ects and their beha iors – Prolog is a logic language( you describe facts

    and the logical relationships among them

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    10/17

    Low level semantics

    • 2emantics can affect things at a ery low le el* – C* d" { x = #$x; } while (x < 1%%);

    Pascal* re!eat x := #$x &ntil x >= 1%%;

    • 'otice that the sense of the test is different* C

    exits the loop when the condition becomesfalse, Pascal when it becomes true

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    11/17

    Syntax supports semantics

    • & language cannot ha e semantics withoutsyntax to support those semantics

    • C couldn’t ha e a f"r loop without syntax• 4a a couldn’t ha e ob3ects without syntax

    for creating and using them• This doesn t mean that f"r loops and

    ob!ects are syntactic constructs"

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    12/17

    Syntax is typographical

    • 2yntax describes the way we write programs asa sequence of characters

    • 2yntax can be precisely and formally defined by .'" 0.ac)us-'aur "orm1

    • & language in the usual sense is a sequence ofcharacters 0or sounds1 and requires syntax

    • .5% you can do many language-li)e thingswith a 65 and no real syntax

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    13/17

    Semantics is fundamental

    • 2emantics affects the ery way we thin) about programming

    • 2omeone once said, 7ou can write a "#$%$&' program in any language/! – %his is a poor way to program – 7ou can use a language, or you can fight with it – f you are fighting with a language, you are either

    • using the wrong language, or • using the language wrong

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    14/17

    Thin#ing in the language

    • n C, functional decomposition is the preferred way to write a program

    • n 4a a, functional decomposition is one ofthe worst ways to write a program

    • n 4a a, you need to* – Choose your set of ob3ects carefully – +ecide the beha iors of each )ind of ob3ect – +ecide how ob3ects communicate and interact

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    15/17

    $oals of this course

    • & primary goal of this course is to introduceyou to different ways of thin)ing about

    programming• t is essential to understand the genius! of a

    language, and to wor) with it, not against it

    • 7ou ha e to learn the syntax of a languagewell enough that you can concentrate on thesemantics

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    16/17

    %ragmatics

    • Pragmatics has to do with how well thelanguage connects to the real world!

    • 2emantics supports pragmatics* some )indsof languages are better for some )inds of

    problems• %he choice of a language should depend on

    pragmatic considerations

  • 8/18/2019 Part 3 - Syntax, Semantics and Pragmatics

    17/17

    Examples of pragmatics

    • C is fast because it does so little error chec)ing• 4a a programs are less buggy because they

    spend so much time on error chec)s• Perl is good for C6 scripts because it has

    powerful tools for string processing• 4a a is a better choice for me than C88

    because )now 4a a better