DOC PREVIEW
UT Arlington CSE 3302 - CSE 3302 Lecture Notes

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302 Programming LanguagesNamesBindingBinding TimeWhere can declarations happen?C++ ExampleScope of BindingExampleDeclaration before UseScope HoleAccess Hidden DeclarationsHide a DeclarationSymbol TableStatic vs. Dynamic ScopeStatic ScopeSlide 16Slide 17Slide 18Slide 19Practice for Static ScopeWhat if dynamic scope is used?Slide 22Slide 23Slide 24Practice for Dynamic ScopeCSE 3302 Programming LanguagesChengkai Li, Weimin HeSpring 2008Semantics1CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Lecture 5 - Semantics, Spring 2008Names•Names: identify language entities–variables, procedures, functions, constants, data types, …•Attributes: properties of names•Examples of attributes:–Data type: int n = 5; ( data type: integer)int itself is a name–Value: ( value: 5)–Location: int* y;y = new int;–Parameters, return value: int f(int n) {...}–…Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200823Binding•Binding: associating attributes to names–declarations–assignments–declarations (prototype) and definition of a function•The bindings can be explicit or implicite.g. int x;–Explicit binding: the data type of x–Implicit binding: the location of x (static or dynamic, depending on where the declaration is)Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Binding Time•Binding Time: the time when an attribute is bound to a name.–Static binding (static attribute): occurs before execution •Language definition/implementation time: The range of data type int•translation time (parsing/semantic analysis): The data type of a variable•link time: The body of external function•load time: Location of global variable–Dynamic binding (dynamic attribute): occurs during execution •entry/exit from procedure or program: the value of local variablesLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20084Where can declarations happen?•Blocks ({}, begin-end, … Algol descendants: C/C++, Java, Pascal, Ada, …)e.g., C–Function body–Anywhere a statement can appear (compound statement)•External/global•Structured data type•ClassLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085C++ Exampleconst int Maximum = 100; struct FullName {string Lastname, string FirstName};class Student { private: struct FullName name; int Age; public: void setValue(const int a, struct FullName name); int TStudent(); …}; void Student::setAge(const int a, string lName, string fName) { int i; Age = a; { int j; name.LastName = lName;name.FirstName = fName; }}…Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20086Scope of Binding•Scope of Binding: the region of the program where the binding is maintained (is valid, applies).•Block-structured languagelexical scope (static scope): from the declaration to the end of the block containing the declaration. dynamic scope : introduced later.Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087ExampleLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20088int x;void p(void) {char y;. . .{ int i; . . .}}void q(void) {double z;. . .}main() {int w[10];. . .}yzwmainqpxiDeclaration before UseLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20089void p(void) {int x;. . .char y;. . .}Exception in OO languages: Scope of local declarations inside a class declaration includes the whole class.public class {public int getValue() { return value; }int value;}yxvalueScope Hole•Scope Hole: Declarations in nested blocks take precedence over the previous declarations. That is, binding becomes invisible/hidden.Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200810int x;void p(void) {char x;x = ‘a’;. . .}main() {x = 2; . . .}x (bound with character data type)x (bound with integer data type)Access Hidden Declarations•scope resolution operator :: (C++)Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200811int x;void p(void) {char x;x = ‘a’;::x=42;. . .}main() {x = 2; . . .}x (bound with character data type)x (bound with integer data type)the hidden integer variable xHide a Declaration•File 1: File 2:extern int x; int x;•File 1: File 2:extern int x; static int x;Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200812Symbol Table•Symbol Table: maintain bindings. Can be viewed as functions that map names to their attributes.Lecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200813Names AttributesSymbolTableStatic vs. Dynamic Scope•Static scope (lexical scope): –scope maintained statically (during compilation) –follow the layout of source codes–used in most languages•Dynamic scope: –scope maintained dynamically (during execution) –follow the execution path–few languages use it (The bindings cannot be determined statically, may depend on user input).•Lisp: considered a bug by its inventor.•Perl: can choose lexical or dynamic scopeLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200814Static ScopeLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200815int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalyStatic ScopeLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200816int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalCharacter, globalydouble, local to pThe symbol table in p: the bindings available in pStatic ScopeLecture 5 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200817int x = 1;char y = ‘a’;void p(void) {double


View Full Document

UT Arlington CSE 3302 - CSE 3302 Lecture Notes

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download CSE 3302 Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CSE 3302 Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CSE 3302 Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?