C language interpreter-15 auxiliary tools for syntax analysis

Source: Internet
Author: User

Before learning about lexical analysis, let's take a look at the definition of words:

/// <summary>    /// A structure for the result of word parsing.    /// </summary>    public class Word    {        public int AbsoluteLine;        public int AbsoluteStartPos;        public int AbsoluteEndPos;        public int Offset;        public int Column;        public string Text;        public bool IsAvailable;        public Context.LocationInfo Location;     }

Note that for ease of calculation, all words are not marked as words, such as operators. Only keywords, variable names, and function names are marked as words.

To better analyze the source code, a SourceCode class is created for lexical analysis:

Public class SourceCode {// various markers public static char [] MultiLineCommentsStartMark = new char [] {'/', '*'}; public static char [] HexMark = new char [] {'x', 'x'}; public static char [] ExponentialMark = new char [] {'E ', 'E'}; public static char [] FloatMark = new char [] {'F', 'F '}; public static char [] PlusMinusMark = new char [] {'-', '+'}; public string Text = ""; public int Line = 0; public int ColumnOfCurrentLine = 0; public int PosOffset = 0; public int LineOffset = 0; public int Column; public Word LastWord; public SourceCode () {} public SourceCode (string txt ); public void LoadFromFile (string path); public bool Eof; // The absolute position of the current position in the entire code public int AbsolutePos; // The absolute line of the current row in the entire code public int AbsoluteLine; public Context. locationInfo Location; public char CurrentChar; // reset the Location index (partial Location index) public void ResetPos (); // check whether the next character matches the specified character with public bool TestNextChar (char c); // check whether the next character is public bool TestNextChar (char [] chars) in the specified character set ); // get the next character. By default, the previous space public void NextChar (bool skipSpace = true) will be skipped; // obtain the remaining code public string Tail from the current position; public static bool IsDigit (char ch); public static bool IsLetter (char ch); public static bool IsSpace (char ch); public static bool IsOperator (char ch ); public static bool IsBracket (char ch); public void SkipSpace (); // use ';' as the separator to divide the code snippet public List <SourceCode> SplitStatement (); // use ',' as the separator to divide multiple variable definition fragments into public List <SourceCode> SplitMultiDeclaration (); // divide the parameter public List <SourceCode> SplitParameter (); // obtain the public static Word GetWord (SourceCode src); // obtain the public static SourceCode GetBracketCode (char leftBracket, char rightBracket, SourceCode src) in the brackets );}

More complex analysis is integrated into the Parser class together with syntax analysis.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.