The display of analysis results of logical expression Lexical analyzer

Source: Internet
Author: User

The previous blog article " Simple logical Expression Lexical Analyzer describes the design of a logical expression lexical analyzer, this time with the lexical analyzer as the backend, the use of VC to build the front-end interface, show the analysis effect.




The result of the analysis is expressed as a tree: the same level of node represents the same rank, and the parameter is higher than the rank of its predicate, for example, the missile level in missile (x) is level 0, and X is 1. After analysis, each node is stored in parser.list, and each node can be inserted into the corresponding position of the tree according to the level property of the node of the linked list.
By analyzing the above logical expressions, the levels of each node are as follows:
Missile x and owns Nono Sub x => sells West x Nono
0 1 0 0 1 1 2 0 0 1 1-1
Visible, using the stack to assist in inserting nodes into the tree is the ideal method. The idea is as follows: The tvi_root of the tree is pressed stack; If the current node is more than its previous node, pressing the previous node to the stack; If the current node level is less than the previous node, the previous node level and the current node level difference between the stack, if the current node level is equal to the previous node level, neither the stack nor stack. The parent node of each current node is the top element of the stack.

The source code is as follows:
void Cppagefaultlogicexpression::onbnclickedbuttonparse ()
... {
TODO: Add control notification Handler code here
UpdateData (TRUE);

if (m_strlogicexpression==l "")
... {
MessageBox (L) Please enter a logical expression. ");
}
Else
... {
M_parser. Parse ((wstring) m_strlogicexpression); Perform lexical analysis
M_bisparsed=true;

/**/////to lexical analysis tree species to add analysis results////
M_tlexer.deleteallitems (); Clear all nodes

Nodelist_it It=m_parser.list.begin ();
Nodelist_it Tmp_it=it;
wchar_t tmp[128];
wcscpy (Tmp,it->content.c_str ());

Stack
Tvinsertstruct Tvinsert;
Tvinsert.hparent = Tvi_root;
Tvinsert.hinsertafter = Tvi_last;
TvInsert.item.mask = Tvif_text;
TvInsert.item.pszText = tmp;

Htreeitem Hcur=tvi_root;
St.push (hcur);
Hcur=m_tlexer.insertitem (&tvinsert); Insert root node

Htreeitem Hprev;
Hprev=hcur;
int level=0,max_level=0;

it++;

For (It;it!=m_parser.list.end (); it++)
... {
wcscpy (Tmp,it->content.c_str ());

if (It->level > Tmp_it->level)/////Current node depth is greater than the previous node, the position of the tree where the previous node is located is in the stack
... {
St.push (Hprev);
Hcur=m_tlexer.insertitem (Tmp,hprev,tvi_last); Inserting a new node

}
else if (It->level < Tmp_it->level)//////////////////////////////
... {
int level=tmp_it->level;
while (It->level < level--)
... {
St.pop ();
}
Hprev=st.top ();
Hcur=m_tlexer.insertitem (Tmp,hprev,tvi_last); Inserting a new node
}
else if (It->level = = tmp_it->level)//The current node depth is equal to the previous node, the new node is inserted according to the top element of the stack
... {
Hprev=st.top ();
&n

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.