UNIT I Programming Language Syntax and semantics B y Kainjan Sanghavi
Contents Bird s eye view of programming language concepts Syntax Semantics Pragmatics
Programming Language Concepts A programming language is a set of rules that provides a way of telling a computer what operations to perform
Bird s eye view of PL Concepts Programming Language Concepts A Simple Program Structure Syntax Semantics Pragmatics Program Organization Data and Algorithms External Environment
Simple Program Structure consists of the following Organization/Structure (Files are arranged) Environment (Defining and Declaring Variables) Computation(Algorithms or functions)
Simple Program Structure consists of the following..example
Syntax Syntax is the required grammar and punctuation of the language Compile-time errors are syntax errors In computer science the syntax of programming language is a set of rules that define the combination of symbols that are considered to be correctly structured programs in that language. Any programming language specifies a set of rules to form valid programs in that language.
Syntax examples FORTRAN 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 keyword integer; C uses int
Another syntax example C: if (x > y) x = x-1; else y--; Pascal: if x > y then x := x-1 else y := y-1; Differences: parentheses around x > y, the word then, = or :=, semicolon before else, - - These are syntactic differences; the meanings are identical
The importance of syntax Correct syntax is obviously important; if you don t get it right, your program won t run In a sense, syntax is trivial; you learn it, you fix it until it s right, end of story But 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 make hard-to-see syntax errors
Examples of poor syntax In FORTRAN, variables don t have to be declared Therefore, every misspelling is a new variable In Pascal, semicolons go between statements Therefore, adding a statement to a block involves adding a semicolon to the previous line
An example of good syntax In Ada, control statements have the form if end if, while end while, case end case, etc. This helps avoid the confusion (in C) resulting from large groups of anonymous closing braces Syntax is usually more important for reading and understanding programs than for writing them
Why syntax matters C: if (x < y) temp = x; x = y; y = temp; Ada: if x < y then temp := x; x := y; y := temp end if; The C version has a bug that almost never occurs in Ada
Semantics Semantics has to do with the meaning of constructs in a language, and the meanings of programs written in that language Semantics is fundamental to everything you do in a language Syntax is just the code you use to describe the semantics
High-level semantics Semantics can affect things at a very high level: C is a procedural language; you describe a set of procedures to follow Java is an object-oriented language; you describe objects and their behaviors Prolog is a logic language; you describe facts and the logical relationships among them
Low level semantics Semantics can affect things at a very low level: C: do { x = 2*x; } while (x < 100); Pascal: repeat x := 2*x until x >= 100; Notice that the sense of the test is different: C exits the loop when the condition becomes false, Pascal when it becomes true
Syntax supports semantics A language cannot have semantics without syntax to support those semantics C couldn t have a for loop without syntax Java couldn t have objects without syntax for creating and using them This doesn t mean that for loops and objects are syntactic constructs!
Semantic Elements Variables Scope, Name, Type, Lifetime Values and References Expressions
VARIABLE In Computer programming a variable is a storage location and associated symbolic name which contains some known or unknown quantity or info, or a value. Whenever a computer needs to store a temporary value in the computer memory for processing and accessing it later then it is store in variable. A Variable is simply a way to store some sort of information for later use.
SEMANTIC properties of variable SCOPE- Which part of program will access to the variable? TYPE- Which type of value /data the variable is holding and what operation should be done? LIFETIME- When Is the variable created and when it is destoryed?
Pragmatics Pragmatics has to do with how well the language connects to the real world Semantics supports pragmatics: some kinds of languages are better for some kinds of problems The choice of a language should depend on pragmatic considerations
Examples of pragmatics C is fast because it does so little error checking Java programs are less buggy because they spend so much time on error checks Perl is good for CGI scripts because it has powerful tools for string processing Java is a better choice for me than C++ because I know Java better
THANKYOU