Lua: Programming III Overview of Lexing& Parsing ...

Lua: Programming III Overview of Lexing & Parsing Introduction to Lexical Analysis

CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 3, 2017

Glenn G. Chappell

Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

? 2017 Glenn G. Chappell

Review Lua: Programming I -- Variables, Values, Expressions

Summary

? Main program is code at global scope. ? String literals use double quotes, single quotes, or double brackets

with a balanced number of equals signs separating each pair of brackets: "abc" and [===[abc]===] are the same. ? Only values have types; variables are references to values. ? Multiple assignment. ? Function print does quick & dirty output. io.write is preferred. ? ".." operator does string concatenation, with automatic number-tostring conversion. ? Type errors are flagged at runtime, when statement is executed.

3 Feb 2017

See prog1.lua.

CS F331 / CSCE A331 Spring 2017

2

Review Lua: Programming I -- Functions

Summary

? A function definition begins with the keyword function. ? Variables default to global--except parameters, loop counter. ? Newlines are irrelevant. ? Call functions as usual. ? First-class functions. ? Also use keyword function to create an unnamed function.

3 Feb 2017

See prog1.lua.

CS F331 / CSCE A331 Spring 2017

3

Review Lua: Programming I -- Tables

Summary

? Maps/dictionaries, arrays, objects, classes implemented using a single PL feature: table, a key-value structure implemented internally as a hash table.

? Table literals use braces, entries separates by commas. Key-value pair is key in brackets, equals sign, value: { ..., ["a"]=56, ... }

? Access values using braces for index syntax, as in C++/Java. ? Delete a key by setting associated value to nil. ? Can mix types of keys, values. ? If a key looks like an identifier, then we can use dot syntax:

t["abc"] and t.abc are the same. ? Can put functions in tables. ? Make an array by listing values in braces without keys. Indices start

at one. arr = { 7, "abc", fibo, 5.34 } ? Length of array arr: #arr

See prog1.lua.

3 Feb 2017

CS F331 / CSCE A331 Spring 2017

4

Review Lua: Programming II -- Flow of Control

Summary

? if COND then STMTS end

? if COND then STMTS else STMTS end

? if COND then STMTS elseif COND then STMTS ... end

? while COND do STMTS end

? for VAR=FIRST, LAST do STMTS end

? for VAR=FIRST, LAST, STEP do STMTS end

Note!

? break: as in C++, Java (there is no "continue")

? Iterator-based for-in loop. Examples:

? for k, v in pairs(TABLE) do STMTS end

? for k, v in ipairs(TABLE_AS_ARRAY) do STMTS end

We will eventually write our own iterators.

? Note: not-equal operator: "~=".

3 Feb 2017

See prog2.lua.

CS F331 / CSCE A331 Spring 2017

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download