Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 03: Structure of Programming Languages Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 2 jgs Structure of a Programming Language Paradigm Language Rules Lexical Input: Symbols Output: Words Syntax Input: Words Output: Sentences Semantic Input: Sentences

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 3 jgs Lexical Rules - Definition Create words Concatenate symbols until you found a whitespace, an operator, or a delimiter

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 4 jgs Lexical Rules - Algorithm do { String word = ""; char c = getNextCharacter(); if (c is not an operator nor a delimiter nor a whitespace nor EndOfLine) word = word + c; } else { if (word is a literal or word is a keyword or word is an identifier){ // This word is correct J } else { // Houston, we have a lexical error L } word = ""; } } while (fileHasMoreCharacters());

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 5 jgs Lexical Rules - Example int x = ; 5 float 3y = "hello; String@z="9.5”;intx=cse240;if(x> 14) while (5 == 5) if (int a) a = 1; x = x; for ( ; ; );y = 13.45.0;int me =99999000001111222000000111111222223 443483045830948;while { x != 9} ();int {x} = 10;

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 6 jgs Lexical Rules - Example int x = ; 5 float 3y = "hello; String@z = "9.5"; intx = cse240; if ( x > 14) while (5 == 5) if (int a) a = 1; x = x; for ( ; ; ); y = 13.45.0; int me =99999000001111222000000111111222223 443483045830948; while { x != 9} (); int {x} = 10;

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 7 jgs Lexical Rules - Tokens § Identifiers. Names (programmer chosen) for something of interest (variables, functions, methods, classes, etc.) 
 § Keywords. names reserved by the language designer: if, switch, for, int, float, char, while, etc. § Operators. +, *, <, >=, !, &&, ||, etc. § Delimiters. , ; ( ) { } § Literals. 5, 14.5, 'A', "Hello", § Comments. /* ... */, // ...

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 8 jgs Structure of a Programming Language Create sentences by combining words

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 9 jgs Syntactic Rules - Grammar

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 10 jgs Syntactic Rules - Example sum4 = (a1 + a2) * (3*b % 4*b)_ p4rd2 = ((a + a2) * (b3 % b4)) hi (c7 - c8); foo_bar = (a1 + a2 - b3 - b4); (a1 / a2) = (c3 - c4); if (z<4) } while ( 5 ) { if ( 6 ) { } }

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 11 jgs Structure of a Programming Language Paradigm Language Rules Lexical Input: Symbols Output: Words Syntax Input: Words Output: Sentences Semantic Input: Sentences

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 12 jgs Structure of a Programming Language Review that the sentences are meaningful Run-Time Errors

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 13 jgs Semantic Rules 1.Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2.Types. Review that the types of variables match the values assigned to them. 3.Array’s indexes. Review that the indexes are integers. 4.Conditions. Review that all expressions on the conditons return a boolean value. 5.Return type. Review that the value returned by a method match the type of the method. 6.Parameters. Review that the parameters in a method match in type and number with the declaration of the method.

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 14 jgs Semantic Rules Declaration and Unicity: Review for uniqueness and that the variable has been declared before its usage. //Case 1: int i; char j; int m; void method(int n, char c) { int n; short l; i = j; i = m; } //Case 2: int i, j; void method() { int i = 5; int j = i + i; int i = i + i; }

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 15 jgs Semantic Rules 1.Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2.Types. Review that the types of variables match the values assigned to them. 3.Array’s indexes. Review that the indexes are integers. 4.Conditions. Review that all expressions on the conditons return a boolean value. 5.Return type. Review that the value returned by a method match the type of the method. 6.Parameters. Review that the parameters in a method match in type and number with the declaration of the method.

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 16 jgs Semantic Rules Types: Review that the types of variables match the values assigned to them. //Case 3: int x = "hello"; char c = false; boolean foo = c;

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 17 jgs Semantic Rules 1.Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2.Types. Review that the types of variables match the values assigned to them. 3.Array’s indexes. Review that the indexes are integers. 4.Conditions. Review that all expressions on the conditons return a boolean value. 5.Return type. Review that the value returned by a method match the type of the method. 6.Parameters. Review that the parameters in a method match in type and number with the declaration of the method.

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 18 jgs Structure of a Programming Language Run-Time Errors Paradigm Language Rules Lexical Input: Symbols Output: Words Syntax Input: Words Output: Sentences Semantic Input: Sentences

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Fall 2021 | 19 jgs Questions

Slide 20

Slide 20 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.