1 / 31

Parsing Nested Comments in C

Learn how to parse nested comments in C programming language using regular expressions and examples.

jbarrows
Download Presentation

Parsing Nested Comments in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 3302 Programming Languages Semantics Kelly Scott French Spring 2017 Lecture 5 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington

  2. Regular Languages • How would you parse nested comments in ‘C’? • http://blog.ostermiller.org/find-comment CSE3302 Programming Languages, UT-Arlington

  3. Regular Languagess • Parsing HTML with Regular Expressions • Explanation 1: • http://stackoverflow.com/a/1758162/135078 • Explanation 2 (fun): • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags • Explanation 3: Regular expression engines sometimes have features that go beyond a regular language. CSE3302 Programming Languages, UT-Arlington

  4. Regular Languages • Validation email address with Regular Expresssions (RegExs) • https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php • /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD CSE3302 Programming Languages, UT-Arlington

  5. Names • 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) {...} • … CSE3302 Programming Languages, UT-Arlington

  6. Binding Binding: associating attributes to names declarations assignments declarations (prototype) and definition of a function The bindings can be explicit or implicit e.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 2017 6 CSE3302 Programming Languages, UT-Arlington

  7. Binding 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 variables CSE3302 Programming Languages, UT-Arlington

  8. Where 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 • Class CSE3302 Programming Languages, UT-Arlington

  9. Pascal Example – One Pass Program Semantics_One_Pass_Program1; Uses Crt; Procedure DrawLine; {Declaration} Var Counter : Integer; Begin textcolor(green); For Counter := 1 to 10 do Begin Write(chr(196)); {horizontal line} End; End; Begin GotoXy(10,5); DrawLine; {Invocation / Evaluation} Readkey; End. … CSE3302 Programming Languages, UT-Arlington

  10. Pascal Example – One Pass Program Semantics_One_Pass_Program1_Error; Uses Crt; Begin GotoXy(10,5); DrawLine; {Invocation} Readkey; End; Procedure DrawLine; {Declaration} Var Counter : Integer; Begin textcolor(green); For Counter := 1 to 10 do Begin Write(chr(196)); {horizontal line} End; End. CSE3302 Programming Languages, UT-Arlington

  11. C++ Example const 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; } } … CSE3302 Programming Languages, UT-Arlington

  12. Scope of Binding • Scope of Binding: the region of the program where the binding is maintained (is valid, applies). • Block-structured language lexical scope (static scope):from the declaration to the end of the block containing the declaration. dynamic scope : introduced later. CSE3302 Programming Languages, UT-Arlington

  13. y i x p z q main w Example int x; void p(void) { char y; . . . { int i; . . . } } void q(void) { double z; . . . } main() { int w[10]; . . . } CSE3302 Programming Languages, UT-Arlington

  14. Declaration before Use void 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; } x y value CSE3302 Programming Languages, UT-Arlington

  15. x (bound with character data type) x (bound with integer data type) Scope Hole • Scope Hole: Declarations in nested blocks take precedence over the previous declarations. That is, binding becomesinvisible/hidden. int x; void p(void) { char x; x = ‘a’; . . . } main() { x = 2; . . . } CSE3302 Programming Languages, UT-Arlington

  16. Access Hidden Declarations • scope resolution operator :: (C++) int 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 x CSE3302 Programming Languages, UT-Arlington

  17. Hide a Declaration • File 1: File 2: extern int x; int x; • File 1: File 2: extern int x; static int x;  CSE3302 Programming Languages, UT-Arlington

  18. Symbol Table • Symbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. SymbolTable Names Attributes CSE3302 Programming Languages, UT-Arlington

  19. Static 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 scope 19 CSE3302 Programming Languages, UT-Arlington

  20. Example of Dynamic Scope in Perl http://perldoc.perl.org/perlfaq7.html#What's-the-difference-between-dynamic-and-lexical-(static)-scoping%3f-Between-local()-and-my()%3f sub visible { print "var has value $var\n"; } sub dynamic { local $var= 'local'; # new temporary value for the still-global visible(); # variable called $var } sub lexical { my $var= 'private'; # new private variable, $var visible(); # (invisible outside of sub scope) } $var= 'global'; visible(); # prints global dynamic(); # prints local lexical(); # prints global CSE3302 Programming Languages, UT-Arlington

  21. Static Scope int 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(); } x y integer, global character, global CSE3302 Programming Languages, UT-Arlington

  22. Static Scope int 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(); } The symbol table in p: the bindings available in p double, local to p x y integer, global Character, global CSE3302 Programming Languages, UT-Arlington

  23. Static Scope int 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(); } The symbol tablein q: the bindings available in q x y integer, global integer, local to q character, global CSE3302 Programming Languages, UT-Arlington

  24. Static Scope int 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(); } The symbol table in main: the bindings available in main character, local to main x y integer, global character, global CSE3302 Programming Languages, UT-Arlington

  25. Static Scope • The symbol table in previous slides are built during compilation • The bindings are used in generating the machine code • Result: 1 a • E.g., semantics of q The symbol tablein q: the bindings available in q x y integer, global void q(void) { int y = 42; printf(“%d\n”,x); p(); } integer, local to q character, global CSE3302 Programming Languages, UT-Arlington

  26. Practice for Static Scope Point 1 Point 2 Point 3 int x,y; void g(void) { x = x + 1; y = x + 1; } void f(void) { int x; y = y + 1; x = y + 1; g(); } main() { x = 1; y = 2; f(); g(); printf("x=%d,y=%d\n",x,y); } Question 1: Draw the symbol table at the given points in the program, using static scope? Question 2: What does the program print, using static scope? 26 CSE3302 Programming Languages, UT-Arlington

  27. What if dynamic scope is used? int 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(); } x y integer, 1, global character, ‘a’, global CSE3302 Programming Languages, UT-Arlington

  28. What if dynamic scope is used? int 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(); } The symbol table in main: the bindings available in main character, ‘b’, local to main x y integer, 1, global character, ‘a’, global CSE3302 Programming Languages, UT-Arlington

  29. What if dynamic scope is used? int 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(); } The symbol tablein q: the bindings available in q character, ‘b’, local to main x y integer, 1, global integer, 42, local to q character, ‘a’, global CSE3302 Programming Languages, UT-Arlington

  30. What if dynamic scope is used? int 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(); } The symbol table in p: the bindings available in p double, 2.5, local to p character, ‘b’, local to main x y integer, 1, global integer, 42, local to q character, ‘a’, global CSE3302 Programming Languages, UT-Arlington

  31. Practice for Dynamic Scope Point 1 Point 2 Point 3 int x,y; void g(void) { x = x + 1; y = x + 1; } void f(void) { int x; y = y + 1; x = y + 1; g(); } main() { x = 1; y = 2; f(); g(); printf("x=%d,y=%d\n",x,y); } Question 1: Draw the symbol table at the given points in the program, using dynamic scope? Question 2: What does the program print, using dynamic scope? 31 CSE3302 Programming Languages, UT-Arlington

More Related