Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
The Z ̃sec Language
You are to build a lexer and a parser for Z ̃sec.
2
§1 §2
§3 Z ̃sec has two types of comments. First, any text, other than a newline, following # to the end of the line isacomment.Second,anytext,includingnewlines,enclosedwithin/# … #/isacomment,andmayspan multiple lines.
§4 An identifier starts with a letter, followed by an arbitrary number of underscores, letters, or digits. Identifiers are case-sensitive. Punctuation other than underscore is not allowed.
§5 Z ̃sec is a security typed language3. Every primitive type must be labelled as either low or high4. A low label is declared with L and a high with H. Information is allowed to flow from L to H, but not vice versa. Security labels allow the compiler to check whether confidential information has been leaked publicly. Checking that the information flow is correct is not part of the coursework. We let S range over H and L in the remainder of this specification.
§7 The boolean constants are T and F and have type bool.
§8 Numbers are integers (type int S), rationals (type rat S), or floats (type float S). Negative numbers are
represented by the ’-’ symbol before the digits. Examples of integers include 1 and -1234; examples of rationals include 1/3 and -345_11/3; examples of floats are -0.1 and 3.14.
§9 Sequences (type seq) are ordered containers of elements. Sequences have nonnegative length. A sequence has a type: its declaration specifies the type of elements it contains. For instance, l : seq<int S > := [1,2,3], and str : seq<char S > := [ ’f’, ’r’, ’e’, ’d’, ’d’, ’y’]. You can use the top S keyword to specify a sequence that contains any type, writing s : seq<top S > := [ 1, 1/2, 3.14, [ ’f’, ’o’, ’u’, ’r’] ].Thezerolengthlistis[].
§10 Z ̃sec sequences support the standard indexing syntax. For any sequence s, the operator len(s) : seq → N returns the length of s and the indices of s range from 0 to len(s)-1. The expression s[index] returns theelementinsatindex.Stringliteralsaresyntacticsugarforcharactersequences,so”abc”is[ ’a’, ’b’ , ’c’].Stringliteralsaretreatedaslowbydefault:theydonotrequireasecurityannotationS.Forthesequence s : seq<charL> := “hello world”,s[len(s)-1]returns‘d’ands[len(s)-1]returns‘d’and len(s) returns 11.
§11 Sequences in Z ̃sec also support sequence slicing as in languages like Python or Ruby: id[i:j] re- turns another sequence, which is a subsequence of id starting at id[i] and ending at id[j]. Given a := [1,2,3,4,5], a[1:3] is [2,3,4]. When the start index is not given, it implies that the subsequence starts from index 0 of the original sequence (e.g., a[:2] is [1,2]). Similarly, when the end index is not given, the subsequence ends with the last element of the original sequence (e.g., a[3:] is [4,5]). Finally, indices can be negative, in which case its value is determined by counting backwards from the end of the original sequence: a[2:-1] is equivalent to a[2:a.len-1] and, therefore, is [3,4,5], while s[-2] is 4. The lower index in a slice must be positive and smaller than the upper index, after the upper index has been subtracted from the sequences length if it was negative.
4Language-Based Information-Flow Security, Sabelfeld and Myers, 2003.
A program in Z ̃sec consists of a list of declarations which defines global variables and new data types as well as functions. This list cannot be empty: it must have a definition for a main function.