{"id":3207,"date":"2021-12-21T11:52:33","date_gmt":"2021-12-21T06:22:33","guid":{"rendered":"https:\/\/binaryterms.com\/?p=3207"},"modified":"2022-04-28T13:05:00","modified_gmt":"2022-04-28T07:35:00","slug":"syntax-analysis","status":"publish","type":"post","link":"https:\/\/binaryterms.com\/syntax-analysis.html","title":{"rendered":"Syntax Analysis in Compiler"},"content":{"rendered":"<p>The syntax analysis phase is the second phase of a compiler. It takes input from the lexical analyzer. And provides an output that serves as input to the semantic analyzer.<\/p>\n<p>Syntax analysis is also referred to as syntax analyzer or parser. It reads the string of tokens from the lexical analyzer. And confirm that it can be generated from the grammar used for the source language.<\/p>\n<h2>Content: Syntax Analysis in Compiler Design<\/h2>\n<ol>\n<li><a href=\"#WhatisSyntaxAnalysis?\">What is Syntax Analysis?<\/a><\/li>\n<li><a href=\"#TypesofParser\">Types of Parser<\/a><\/li>\n<li><a href=\"#SyntaxErrorHandling\">Syntax Error Handling<\/a><\/li>\n<li><a href=\"#ErrorRecoverMethod\">Error Recover Method<\/a><\/li>\n<li><a href=\"#KeyTakeaways\">Key Takeaways<\/a><\/li>\n<\/ol>\n<p><a name=\"WhatisSyntaxAnalysis?\"><\/a><\/p>\n<h3>What is Syntax Analysis?<\/h3>\n<p>The syntax analysis phase gets its input from the lexical analyzer which is a string of tokens. It verifies whether the provided string of tokens is grammatically correct or not.<\/p>\n<ul>\n<li>If that string of tokens is grammatically incorrect, it reports the syntax error.<\/li>\n<li>If that string of tokens is grammatically correct, it produces a parse tree for that string.<\/li>\n<\/ul>\n<p>Later the syntax analyzer forwards this parse tree to the next front end for processing. We also refer to the syntax analyzer as the parser.<\/p>\n<p>Besides building the parse tree syntax analyzer even collects information about each token. And stores this information in the symbol table. Along with this it even performs:<\/p>\n<ul>\n<li>Type checking.<\/li>\n<li>Does semantic analysis.<\/li>\n<li>Generates an intermediate code.<\/li>\n<\/ul>\n<p>In case, syntax analyzer identifies syntax errors, it performs error recovery methods. These methods help syntax analyzers to handle the syntax error.<\/p>\n<p><a name=\"TypesofParser\"><\/a><\/p>\n<h3>Types of Parsing<\/h3>\n<p>The three common types of parsing are as follow:<\/p>\n<ol>\n<li>Universal Parsing<\/li>\n<li>Top-down Parsing<\/li>\n<li>Bottom-up Parsing<\/li>\n<\/ol>\n<p><strong>Universal Parsing<\/strong><\/p>\n<p>Though universal parsing can parse any type of grammar. But it is quite ineffective to be used in the production compiler. So usually, we only use two methods for parsing top-down and bottom.<\/p>\n<p><strong>Top-down Parsing<\/strong><\/p>\n<p>In the top-down method, the parser builds the parse tree starting from the top. That means it starts from the root of the parse tree, traversing towards the bottom i.e. the leaves of the parse tree.<\/p>\n<p><strong>Bottom-up Parsing<\/strong><\/p>\n<p>In the bottom-up method, the parser builds the parse tree starting from the bottom. This implies it starts from the leaves of the parse tree, traversing upwards to the top i.e. root of the parse tree.<\/p>\n<p><strong>Note<\/strong>: Whatever type of parsing, the parser chooses, it starts scanning the parse tree from the left. And it will continue traversing the tree towards the right. Remember it will scan only one symbol or node at a time.<br \/>\n<a name=\"SyntaxErrorHandling\"><\/a><\/p>\n<h3>Syntax Error Handling<\/h3>\n<p>The compiler is specially designed to assist the programmer in tracking down the errors. To gain syntactic accuracy error handling is left to the compiler designer.<\/p>\n<ul>\n<li>If we concentrate on error handling right from the starting of compiler designing:<\/li>\n<li>It will help in simplifying the structure of the compiler.<\/li>\n<li>And it will also improve its error handling capability.<\/li>\n<\/ul>\n<h4>Different Types of Errors<\/h4>\n<ol>\n<li><strong>Lexical Error<\/strong>: It occurs when spellings of the identifiers, keywords, operators are misspelt.<\/li>\n<li><strong>Syntactic Error<\/strong>: It arises when you forget to put semicolons, braces, commas etc.<\/li>\n<li><strong>Semantic Error<\/strong>: This occurs when there is a mismatch between operators and operands.<\/li>\n<li><strong>Logical Error<\/strong>: It occurs when the programmer provides incorrect reasoning in the program.<\/li>\n<\/ol>\n<p>However, the main task of the parser is to detect syntactic error efficiently. The error handling in parser involves:<\/p>\n<ul>\n<li>The parser must report the errors in the program accurately.<\/li>\n<li>The parser must recover from each error quickly. So that it can detect subsequent errors.<\/li>\n<li>There must be minimal overhead to process a correct program.<\/li>\n<\/ul>\n<p>The error handler must report the location of the error in the program. This helps the compiler to detect the line at which an error had occurred. Also, it points to the line at which error occurs.<br \/>\n<a name=\"ErrorRecoverMethod\"><\/a><\/p>\n<h3>Error Recovery Method<\/h3>\n<p>Even though developers design programming languages with error handling capability. But the errors are inevitable in the programs despite the programmer\u2019s efforts. Thus, compilers are designed to track down and locate the errors in the program.<\/p>\n<p>It is the parser that efficiently detects a syntactic error from the program. After detecting the error, the parser must correct or recover that error.<\/p>\n<p>With the first approach, the parser can quit after detecting the first error. Thereby it leaves an informative message describing the error location.<\/p>\n<p>But if the errors keep on rising, the compiler must give up after exceeding a particular limit. As it is not useful to create an avalanche of errors.<\/p>\n<h4>Error Recovery Strategies:<\/h4>\n<p><strong>1. Panic-Mode Recovery<br \/>\n<\/strong>Here, the parser discards the input symbol one at a time until it discovers a synchronizing token. Usually, the synchronizing tokens are the delimiters. Though this method is simple. Yet it skips a large amount of input without scanning it for additional errors.<\/p>\n<p><strong>2. Phrase-Level Recovery<br \/>\n<\/strong>In phrase-level recovery, the parser performs a local correction on the remaining input.<\/p>\n<p>Once the parser encounters an error it replaces the prefix of the remaining input with a string. And this is done in such a way that the parser should not stop parsing and must continue.<\/p>\n<p>Well, the choice of replacement is left to the compiler designer. But it must be taken care that the replacement should not lead to an infinite loop. This method can correct almost any input string. Still, this method is not able to recover from an error that has occurred before the point of detection.<\/p>\n<p><strong>3. Error Production<\/strong><br \/>\nThere are some erroneous productions that commonly occurs. These error productions are augmented with the grammar for a language. This facilitates the parser to detect the anticipated error.<\/p>\n<p><strong>4. Global Correction<\/strong><br \/>\nHere, the parser must make least changes while correcting an invalid string to a valid string. Although these methods are costly to implement and is only in theories.<\/p>\n<p><a name=\"KeyTakeaways\"><\/a><\/p>\n<div id=\"keytake\">\n<h3>Key Takeaway<\/h3>\n<ul>\n<li>Syntax analysis is the second phase of a compiler design.<\/li>\n<li>The input provided to the syntax analyzer is a stream of tokens from the lexical analyzer.<\/li>\n<li>Parser verifies if the provided stream of tokens is as per the grammar for the source language.<\/li>\n<li>If no syntax error occurs in the input string of tokens, the parser generates a parse tree for the same.<\/li>\n<li>If syntax error occurs then the parser handles it by applying error recovery methods.<\/li>\n<\/ul>\n<\/div>\n<p>So, this is all about the syntax analysis phase of the compiler. We have learned about its functions in the compiler. Also, we got to know how it examines the input string to verify if there are any syntactical errors present or not.<\/p>\n<p>We have also discussed how the parser corrects the error with error recovery methods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The syntax analysis phase is the second phase of a compiler. It takes input from the lexical analyzer. And provides an output that serves as input to the semantic analyzer. Syntax analysis is also referred to as syntax analyzer or parser. It reads the string of tokens from the lexical analyzer. And confirm that it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[13],"tags":[],"class_list":{"0":"post-3207","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-compiler-design","7":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is Syntax Analysis in Compiler? Definition, Types, Error Handling &amp; Recovery - Binary Terms<\/title>\n<meta name=\"description\" content=\"The syntax analysis phase is the second phase of a compiler it takes input from the lexical analyzer and provides an output to the semantic analyzer.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/binaryterms.com\/syntax-analysis.html\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Syntax Analysis in Compiler? Definition, Types, Error Handling &amp; Recovery - Binary Terms\" \/>\n<meta property=\"og:description\" content=\"The syntax analysis phase is the second phase of a compiler it takes input from the lexical analyzer and provides an output to the semantic analyzer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/binaryterms.com\/syntax-analysis.html\" \/>\n<meta property=\"og:site_name\" content=\"Binary Terms\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-21T06:22:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-28T07:35:00+00:00\" \/>\n<meta name=\"author\" content=\"Neha T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Neha T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/binaryterms.com\/syntax-analysis.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/binaryterms.com\/syntax-analysis.html\"},\"author\":{\"name\":\"Neha T\",\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe\"},\"headline\":\"Syntax Analysis in Compiler\",\"datePublished\":\"2021-12-21T06:22:33+00:00\",\"dateModified\":\"2022-04-28T07:35:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/binaryterms.com\/syntax-analysis.html\"},\"wordCount\":1079,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/binaryterms.com\/#organization\"},\"articleSection\":[\"Compiler Design\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/binaryterms.com\/syntax-analysis.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryterms.com\/syntax-analysis.html\",\"url\":\"https:\/\/binaryterms.com\/syntax-analysis.html\",\"name\":\"What is Syntax Analysis in Compiler? Definition, Types, Error Handling & Recovery - Binary Terms\",\"isPartOf\":{\"@id\":\"https:\/\/binaryterms.com\/#website\"},\"datePublished\":\"2021-12-21T06:22:33+00:00\",\"dateModified\":\"2022-04-28T07:35:00+00:00\",\"description\":\"The syntax analysis phase is the second phase of a compiler it takes input from the lexical analyzer and provides an output to the semantic analyzer.\",\"breadcrumb\":{\"@id\":\"https:\/\/binaryterms.com\/syntax-analysis.html#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryterms.com\/syntax-analysis.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryterms.com\/syntax-analysis.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryterms.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Syntax Analysis in Compiler\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryterms.com\/#website\",\"url\":\"https:\/\/binaryterms.com\/\",\"name\":\"Binary Terms\",\"description\":\"The Computer Science &amp; IT Guide\",\"publisher\":{\"@id\":\"https:\/\/binaryterms.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryterms.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/binaryterms.com\/#organization\",\"name\":\"Binary Terms\",\"url\":\"https:\/\/binaryterms.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png\",\"contentUrl\":\"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png\",\"width\":400,\"height\":63,\"caption\":\"Binary Terms\"},\"image\":{\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe\",\"name\":\"Neha T\",\"url\":\"https:\/\/binaryterms.com\/author\/author\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Syntax Analysis in Compiler? Definition, Types, Error Handling & Recovery - Binary Terms","description":"The syntax analysis phase is the second phase of a compiler it takes input from the lexical analyzer and provides an output to the semantic analyzer.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/binaryterms.com\/syntax-analysis.html","og_locale":"en_GB","og_type":"article","og_title":"What is Syntax Analysis in Compiler? Definition, Types, Error Handling & Recovery - Binary Terms","og_description":"The syntax analysis phase is the second phase of a compiler it takes input from the lexical analyzer and provides an output to the semantic analyzer.","og_url":"https:\/\/binaryterms.com\/syntax-analysis.html","og_site_name":"Binary Terms","article_published_time":"2021-12-21T06:22:33+00:00","article_modified_time":"2022-04-28T07:35:00+00:00","author":"Neha T","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Neha T","Estimated reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/binaryterms.com\/syntax-analysis.html#article","isPartOf":{"@id":"https:\/\/binaryterms.com\/syntax-analysis.html"},"author":{"name":"Neha T","@id":"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe"},"headline":"Syntax Analysis in Compiler","datePublished":"2021-12-21T06:22:33+00:00","dateModified":"2022-04-28T07:35:00+00:00","mainEntityOfPage":{"@id":"https:\/\/binaryterms.com\/syntax-analysis.html"},"wordCount":1079,"commentCount":0,"publisher":{"@id":"https:\/\/binaryterms.com\/#organization"},"articleSection":["Compiler Design"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/binaryterms.com\/syntax-analysis.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/binaryterms.com\/syntax-analysis.html","url":"https:\/\/binaryterms.com\/syntax-analysis.html","name":"What is Syntax Analysis in Compiler? Definition, Types, Error Handling & Recovery - Binary Terms","isPartOf":{"@id":"https:\/\/binaryterms.com\/#website"},"datePublished":"2021-12-21T06:22:33+00:00","dateModified":"2022-04-28T07:35:00+00:00","description":"The syntax analysis phase is the second phase of a compiler it takes input from the lexical analyzer and provides an output to the semantic analyzer.","breadcrumb":{"@id":"https:\/\/binaryterms.com\/syntax-analysis.html#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryterms.com\/syntax-analysis.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/binaryterms.com\/syntax-analysis.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryterms.com\/"},{"@type":"ListItem","position":2,"name":"Syntax Analysis in Compiler"}]},{"@type":"WebSite","@id":"https:\/\/binaryterms.com\/#website","url":"https:\/\/binaryterms.com\/","name":"Binary Terms","description":"The Computer Science &amp; IT Guide","publisher":{"@id":"https:\/\/binaryterms.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryterms.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/binaryterms.com\/#organization","name":"Binary Terms","url":"https:\/\/binaryterms.com\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/","url":"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png","contentUrl":"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png","width":400,"height":63,"caption":"Binary Terms"},"image":{"@id":"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe","name":"Neha T","url":"https:\/\/binaryterms.com\/author\/author"}]}},"_links":{"self":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts\/3207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/comments?post=3207"}],"version-history":[{"count":12,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts\/3207\/revisions"}],"predecessor-version":[{"id":3284,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts\/3207\/revisions\/3284"}],"wp:attachment":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/media?parent=3207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/categories?post=3207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/tags?post=3207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}