简体   繁体   中英

Solidity: ParserError: Expected pragma, import directive or contract /interface/library definition

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

I have tried following steps

  1. uninstalled Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25
  3. node compile.js (code given below)

     { contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }

Compile.js

const path  = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src =  fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);

console.log(res);

Inbox.sol

pragma solidity ^0.4.25;

contract Inbox {
    string  message;


    function Inbox(string passedName) public {
        message = passedName;
    } 

    function setMessage(string newMsg) public {
        message = newMsg;
    }

    function getMessage() public view returns(string){
        return message;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

I have tried following steps

  1. uninstalled Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25
  3. node compile.js (code given below)

     { contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }

Compile.js

const path  = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src =  fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);

console.log(res);

Inbox.sol

pragma solidity ^0.4.25;

contract Inbox {
    string  message;


    function Inbox(string passedName) public {
        message = passedName;
    } 

    function setMessage(string newMsg) public {
        message = newMsg;
    }

    function getMessage() public view returns(string){
        return message;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

I have tried following steps

  1. uninstalled Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25
  3. node compile.js (code given below)

     { contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }

Compile.js

const path  = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src =  fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);

console.log(res);

Inbox.sol

pragma solidity ^0.4.25;

contract Inbox {
    string  message;


    function Inbox(string passedName) public {
        message = passedName;
    } 

    function setMessage(string newMsg) public {
        message = newMsg;
    }

    function getMessage() public view returns(string){
        return message;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

I have tried following steps

  1. uninstalled Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25
  3. node compile.js (code given below)

     { contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }

Compile.js

const path  = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src =  fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);

console.log(res);

Inbox.sol

pragma solidity ^0.4.25;

contract Inbox {
    string  message;


    function Inbox(string passedName) public {
        message = passedName;
    } 

    function setMessage(string newMsg) public {
        message = newMsg;
    }

    function getMessage() public view returns(string){
        return message;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

I have tried following steps

  1. uninstalled Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25
  3. node compile.js (code given below)

     { contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }

Compile.js

const path  = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src =  fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);

console.log(res);

Inbox.sol

pragma solidity ^0.4.25;

contract Inbox {
    string  message;


    function Inbox(string passedName) public {
        message = passedName;
    } 

    function setMessage(string newMsg) public {
        message = newMsg;
    }

    function getMessage() public view returns(string){
        return message;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.

ex:   function setMessage(string **memory** newMsg) 

Check if your .sol file is encoded in UTF-8 without BOM.

Otherwise, convert to UTF-8.

I used Notepad++ to do that.

Encoding > Convert to UTF-8

Parse errors happen when you make an error in the syntax of your program. I got the same error because

contract ERC721Enumerable is ERC721{

    function totalSupply() public view returns (uint256){
        return _allTokens.length;
    }
// THIS WAS THE REASON
// this was closing contract and then outside of the contract i had function
}

    function _mint(address to, uint256 tokenId) internal override {
       
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM