Cs802 compilers spring 2017 محاضرة 5

Page 1

‫‪Spring 2017‬‬

‫‪CS802: Compliers‬‬

‫‪Dr. Mabroka A. M.‬‬

‫‪ ‬مرحلة اللغوي (النحوي) )‪syntax analysis (parsing‬‬ ‫ُ‬ ‫في هذه املرحلة‪ ،‬يتم التحقق من ان البرنامج املصدر قد كتب بطريقة نحوية صحيحة‪.‬‬

‫‪Position of parser in compiler model‬‬

‫‪ ‬املحلل اللغوي (النحوي) )‪syntax analyzer (parser‬‬ ‫يقوم املحلل اللغوي بتجميع الرموز ‪ tokens‬التي تحصل عليها من املحلل اللفظي ‪lexical analyzer‬‬ ‫بطريقة تسلسلية هرمية بحيث تعطي معنى‪.‬‬ ‫اذن‪ ،‬على املحلل اللغوي ان يتبع قواعد معينة للتحقق من أن مجموعة ال‪ tokens‬مكتوبة في صيغة‬ ‫صحيحة‪ .‬و من القواعد النحوية العامة التي يستخدمها ال‪ syntax‬في صياغة نحو اللغة‪ ،‬او في توضيح‬ ‫نحو اللغة هي قاعدة املقام‪-‬الحر ‪ context-free grammar‬التي يرمز لها بالرمز‪:‬‬ ‫)‪BNF (Backus-Naur Form‬‬ ‫ُ‬ ‫و هي تستخدم لوصف الهيكلية الهرمية النواع مختلفة من اللغات‪.‬‬ ‫‪Context-free grammar (BNF) ‬‬ ‫تتكون من التالي‪:‬‬ ‫ُ‬ ‫‪ :Terminal .1‬هي الرموز االساسية التي تصاغ منها السالسل‪ ،‬مرادفها ‪ ،token‬و هي اصغر وحدة‬ ‫في البرنامج لها معنى مثل‪.id, if, int, for :‬‬ ‫‪ :Nonterminal .2‬هي املتغيرات النحوية التي تدل على فئات السالسل مثل التعليمات‬ ‫‪ ،instructions‬التعابير الرياضية ‪:math expression‬‬ ‫‪if‬‬ ‫; ‪(condition) statement‬‬ ‫ً‬ ‫‪ :Start symbol .3‬هو رمز ُيعطي لعنونة القاعدة النحوية و هو دائما ‪.nonterminal‬‬ ‫‪30‬‬


Dr. Mabroka A. M.

CS802: Compliers

Spring 2017

‫ يحتوي على الجانب االيسر‬:Production or set of productions .4 .nonterminal ‫ أو‬terminal ‫ يليه فئة من‬،‫( متبوع بسهم‬nonterminal or start symbol) grammar of programming language ‫ قواعد لغات البرمجة‬ .expression ‫ أو تعبير‬statements or instructions ‫أي لغة برمجة أما ان تتكون من جمل‬ .‫ البد من وجود قواعد نحوية لكل منهما‬،‫اذن‬ ً ‫ التعبير الرياض ي يتكون من العمليات االربع االساسية و هي ؟؟؟‬:syntax of expression :‫أوال‬ ‫ و في‬،‫ تتم العملية الحسابية حسب االسبقية مع مراعاة االقواس كما في لغة الجافا‬،‫في بعض اللغات‬ .‫لغات معينة تتم حسب ترتيب العمليات من اليسار الى اليمين كما في لغة البيسك‬ eg: write BNF for the following expression: 5+3-7 Ans. according to BNF, the operations: 5+3-7 then, exp digit 5+3

-7

???? ??? ???  Question: write the BNF for expression which consists of ( ),*,/,+,- with respect to given sequences.

31


Dr. Mabroka A. M.

CS802: Compliers

Spring 2017

syntax tree ‫ الشجرة النحوية‬ ‫ لها جذر معنون‬،‫الشجرة النحوية هي عبارة عن ترتيب لعناصر محددة بشكل يحاكي شجرة في الطبيعة‬ ‫ و كل عقدة‬.nonterminal ‫ او‬terminal ‫ أو‬،token‫ و كل فرع فيها معنون ب‬،start symbol‫ب‬ ‫ الشجرة النحوية ملتابعة تسلسل عناصر‬syntax‫ و يستخدم ال‬.nonterminal‫داخلية معنونة ب‬ .‫التعبير الرياض ي بطريقة صحيحة‬ There are three general types of parsers for grammars: universal, top-down, and bottom-up. Universal parsing methods such as the Cocke-Younger-Kasami algorithm and Earley's algorithm can parse any grammar. These general methods are, however, too inefficient to use in production compilers. The methods commonly used in compilers can be classified as being either top-down or bottom-up. As implied by their names, top-down methods build parse trees from the top (root) to the bottom (leaves), while bottom-up methods start from the leaves and work their way up to the root. In either case, the input to the parser is scanned from left to right, one symbol at a time.

eg. with respect to sequences. Write BNF, then, draw the syntax tree for the following expressions: 1. 8*2-5+1 2. (7-3)+4 3. 9-1*(4+2)/3 4. a-(c/b)*a

32


‫‪Spring 2017‬‬

‫‪CS802: Compliers‬‬

‫‪Dr. Mabroka A. M.‬‬

‫ً‬ ‫ثانيا‪ :Syntax of statement :‬يتعرف ال‪ syntax‬على الجمل ‪ statements‬في معظم لغات البرمجة‬ ‫عن طريق الكلمات املحجوزة‪ ،‬فكما في لغة ‪ ،C++‬الكلمة املحجوزة ‪ main‬تدل على بداية البرنامج‬ ‫الرئيس ي و كذلك قوس البداية و النهاية يدالن على ‪ block‬واحد‪ ،‬و هكذا‪.‬‬ ‫ً‬ ‫وفقا ل‪ ،BNF‬فان قاعدة ‪ if‬تكون بالشكل التالي‪:‬‬ ‫‪Stmt‬‬ ‫;‪if (condition) statement‬‬ ‫‪the syntax tree of following statement :‬‬ ‫;‪if (a<=b) min=a‬‬

‫‪33‬‬


‫‪Spring 2017‬‬

‫‪CS802: Compliers‬‬

‫‪Dr. Mabroka A. M.‬‬

‫‪Syntax-directed Translation ‬‬ ‫ُيستخدم لتحديد الترجمة (التحويالت) من حالة الى أخرى للبنيات التي تتكون منها لغة البرمجة‪ .‬و‬ ‫معنى ذلك أنها تحدد ترجمة البنية بخصوص الصفات ثم تنسيقها مع مكوناتها النحوية‪.‬‬ ‫يعتبر ‪ syntax-directed translation‬تعميم ل‪ BNF‬بحيث ان كل قاعدة نحوية لها ما يقابلها من فئة‬ ‫من الصفات اما ان تكون هذه الصفات تركيبية او مورثة‪.‬‬ ‫مثل السجل الذي يحتوي على معلومات معينة‪ ،‬تكون صفات لهذا السجل‪.‬‬ ‫‪ ‬الصفات التركيبية ‪synthesized attributes‬‬ ‫ُ‬ ‫تكون الصفات تركيبية اذا كانت قيمتها (العقدة) في الشجرة النحوية تحدد من قبل الفروع لهذه‬ ‫العقدة‪.‬‬ ‫‪ Translation Scheme ‬خطة الترجمة (طريقة الترجمة)‬ ‫و هي تحدد طريقة ترجمة معينة مثل تحويل التعبير الرياض ي من ‪ infix‬الى ‪.postfix‬‬ ‫‪Postfix notation ‬‬ ‫ُيعرف ال‪ postfix notation‬لتعبير رياض ي بما يلي‪:‬‬ ‫‪1. If E is constant or variable, then the postfix notation for E is E itself.‬‬ ‫‪2. If E is expression in form of E1 op E2, op is operation‬‬ ‫‪Then E’1 E’2 op is the postfix notation in such that E’1 is the postfix of E1‬‬ ‫‪E’2 is the postfix of E2.‬‬ ‫‪3. If E is expression in form of (E1) , then the postfix for E1 is same as the postfix‬‬ ‫‪for E.‬‬ ‫‪eg. Suppose 4+2 is expression, then 4+2‬‬ ‫‪postfix for 4 is 4‬‬ ‫‪postfix for 2 is 2‬‬ ‫‪postfix for 4+2 is E1E2 op => 42+‬‬ ‫‪ ‬مالحظة‪ :‬ال وجود لالقواس في ‪ ،postfix notation‬ملاذا؟؟؟‬

‫‪34‬‬


Dr. Mabroka A. M.

CS802: Compliers

Spring 2017

:‫ سؤال املحاضرة‬ :‫ أوجد ما يلي‬،9-2+7 :‫ التعبير الحسابي التالي‬،syntax-directed translation ‫باستخدام‬ Postfix notation .1 Syntax tree .2 Attributes .3 Q. write a pseudo code for translating a simple arithmetic expression from infix to postfix notation.  Parser Generator Yacc stands for "yet another compiler-cpmpiler," reflecting the popularity of parser generators in the early 1970s when the first version of Yacc was created by S. C. Johnson. Yacc is available as a command on the UNIX system, and has been used to help implement many production compilers.  The Parser Generator Yac c A translator can be constructed using Yacc in the manner illustrated below First, a file, say translate . y, containing a Yacc specification of the translator is prepared. The UNIX system command yacc translate . y transforms ttte file trans late . y into a C program called y. tab . c using the LALR method outlined in Algorithm 4.63. The program y. tab! c is a representation of an LALR parser written in C, along with other C routines that the user may have prepared. The LALR parsing table is compacted. By compiling y. tab . c along with the ly library tttat contains the LR parsing program using the command cc y . tab . c –ly we obtain the desired object program a. out that performs the translation specified by the original Yac c program.7 If other procedures are needed, they cap be compiled or loaded with y. tab . c, just as with any C program.

Creating an input/output translator with Yacc

35


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.